问题
I have a table with rows that's grouped if the date and the category 'bar' matches, now I need to INNER JOIN
these with another table and use a WHERE answer = '2'
from that table, but I don't know how to do that.
SELECT category, date, client
FROM sbs_events
WHERE category <> 'bar'
UNION ALL
SELECT category, date, MIN(client) AS client
FROM sbs_events
WHERE category = 'bar'
GROUP BY category, date
I've made a SQL Fiddle
The thing I need to add to the fiddle is
INNER JOIN sbs_userEvents
ON sbs_events.id = sbs_userEvents.event_id
WHERE answer = 2
AND user_id = 1
But since I already have a WHERE category = 'bar'
I don't know how to add this.
回答1:
SELECT category, date, client
FROM sbs_events
INNER JOIN sbs_userEvents ON sbs_events.id = sbs_userEvents.event_id
WHERE category <> 'bar' AND answer = 2 AND user_id = 1
UNION ALL
SELECT category, date, MIN(client) AS client
FROM sbs_events
INNER JOIN sbs_userEvents ON sbs_events.id = sbs_userEvents.event_id
WHERE category = 'bar' AND answer = 2 AND user_id = 1
GROUP BY category, date
来源:https://stackoverflow.com/questions/37324082/mysql-inner-join-together-with-union-all