我有两张桌子如下:
用户表:
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
另一张表为uep,如下所示:
CREATE TABLE `uep` (
`user_id` int(10) unsigned NOT NULL,
`email_template_id` int(10) unsigned NOT NULL,
`sid` int(10) unsigned NOT NULL,
`send_email` tinyint(1) NOT NULL DEFAULT '1'
);
我想从用户表中获取所有记录,并且仅当
left
但是当我运行query时
Select id, group_concat(email_template_id) as T from user
LEFT join uep ON (user.id = uep.user_id AND uep.send_email = 0)
group by user_id limit 100
它忽略用户表中的send_email=1的用户。
我只想在user.id和send_email=0时加入uep表,否则不应该加入,只返回用户详细信息。
Sql小提琴:http://www.sqlfiddle.com/#!2/1f083/2
任何帮助通知!!
最佳答案
测试
Select
id, group_concat(email_template_id) as T
from
user
LEFT join
uep
ON
(user.id = uep.user_id AND uep.send_email = 0)
OR
(uep.send_email = 1 AND user.id = uep.user_id)
group by
user_id
limit
100;
演示时间http://www.sqlfiddle.com/#!2/6c28ee/1