我试图弄清楚如何在MySQL中运行查询以执行以下操作:
搜索以5开头的任何数字,并在标记为classRef的列中得到结果,且结果文本为FOO
如果该数字以5以外的任何数字开头,则将其输出到列classRef,结果文本为BAR。
这是我到目前为止所拥有的:
SELECT
ara.AddressNumber AS ExternalID,
ara.AddressNumber as tranId,
cus.Name AS customerRef,
cus.ExternalID LIKE '5%' AS classRef,
cus.ExternalID NOT LIKE '5%' AS classRef2,
'1' AS itemLine_quantity,
'0' AS itemLine_salesPrice
FROM
adrun_copy ara,
customers_copy cus
WHERE
ara.AAccountNumber = cus.ExternalID
最佳答案
CASE WHEN cus.ExternalID LIKE '5%' THEN 'FOO' ELSE 'BAR' END As classRef
如果确实是像
int
这样的数字列,则可能需要先将其CAST
转换为varchar ...不确定MySql的相关规则。关于mysql - 像和AS文本结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18903140/