mysql条件语句?

时间:2013-07-16 09:33:09

标签: mysql sql

我在sql查询中面临一个小问题,

这是查询

SELECT events.event_id,
( SELECT favorite_events.event_id, 
        (CASE favorite_events.event_id WHEN NULL THEN 0 ELSE 1 END)
        FROM favorite_events 
        WHERE favorite_events.event_id = events.event_id 
) AS is_favorite
FROM events 
WHERE start_date = 2013-07-16

它给了我“操作数应该包含1列 “错误,请帮忙!

2 个答案:

答案 0 :(得分:2)

在子查询中,您选择了2列。您无法将2列重命名为is_favorite

我假设,通过查看查询,您可能不需要字段favorite_events.event_id。只需删除该部分。

答案 1 :(得分:2)

使用JOIN,而不是相关的子查询。

SELECT e.event_id,
       f.event_id IS NOT NULL AS is_favorite
FROM events e
LEFT JOIN favorite_events f
ON f.event_id = e.event_id
WHERE e.start_date = '2013-07-16'