像和文本结果一样

时间:2013-09-19 19:20:05

标签: mysql sql

我正在试图弄清楚如何在MySQL中运行将执行以下操作的查询:

  1. 搜索任何以5开头的数字,并将结果放在标有classRef的列中,结果文字为FOO
  2. 如果该数字以5以外的任何数字开头,则输出到列classRef,结果文本为BAR。
  3. 这是我到目前为止所做的:

    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
    

1 个答案:

答案 0 :(得分:1)

CASE WHEN cus.ExternalID LIKE '5%' THEN 'FOO' ELSE 'BAR' END As classRef

如果那真的是像int那样的数字列,你可能需要CAST首先将它变为varchar ...不确定MySql的规则。