mySQL INNER JOIN together with UNION ALL

2019-09-15 23:24发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-16 00:01
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
查看更多
登录 后发表回答