使用子选择值

时间:2016-11-01 17:59:56

标签: mysql

我想知道是否可以重写以下查询,以便“end_value”列返回“isequal”,“isless”或“isgreater”,具体取决于其他两个动态列的值。

SELECT
  (SELECT cat_id FROM catA WHERE uid = 111) AS user_value,
  (SELECT cat_id FROM catB WHERE oid = 222) AS set_value,
  CASE
    WHEN user_value = set_value THEN 'isequal'
    WHEN user_value < set_value THEN 'isless'
    WHEN user_value > set_value THEN 'isgreater'
  END AS end_value

MySQL目前在“字段列表”中抱怨“未知列'user_value'”。是这样的吗?

由于

1 个答案:

答案 0 :(得分:1)

这个怎么样?

SELECT  catA.cat_id AS user_value, catB.cat_id AS set_value,
        CASE
            WHEN catA.cat_id = catB.cat_id THEN 'isequal'
            WHEN catA.cat_id < catB.cat_id THEN 'isless'
            WHEN catA.cat_id > catB.cat_id THEN 'isgreater'
          END AS end_value
FROM catA, catB
WHERE catA.uid = 111
  AND catB.oid = 222