mySQL INNER JOIN和UNION ALL一起 [英] mySQL INNER JOIN together with UNION ALL

查看:82
本文介绍了mySQL INNER JOIN和UNION ALL一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中如果日期和类别"bar"匹配则对行进行分组,现在我需要对其他表进行 INNER JOIN 并使用 WHERE answer ='2'从该表开始,但我不知道该怎么做.

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

我制作了 SQL小提琴

我需要添加到小提琴中的东西是

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

但是由于我已经有一个 WHERE category ='bar',所以我不知道如何添加它.

But since I already have a WHERE category = 'bar' I don't know how to add this.

推荐答案

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

这篇关于mySQL INNER JOIN和UNION ALL一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆