Irfan Ahmed
Irfan Ahmed

Reputation: 9384

mysql conditional statement?

i am facing a little problem in an sql query,

here is the query

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

it is giving me "Operand should contain 1 column(s) " error, please help!

Upvotes: 1

Views: 308

Answers (2)

Barmar
Barmar

Reputation: 782574

Use a JOIN, not a correlated subquery.

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'

Upvotes: 2

Rene Pot
Rene Pot

Reputation: 24815

In the subquery you are selecting 2 columns. You can't rename 2 colums as is_favorite.

I assume, by looking at the query, you probably don't need the field favorite_events.event_id. Just remove that part.

Upvotes: 2

Related Questions