mySQL INNER JOIN together with UNION ALL

别说谁变了你拦得住时间么 提交于 2020-01-07 02:55:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!