SELECT user.id, CONCAT(lastname, firstname), year, month, date FROM user
LEFT JOIN adoption ON user.id = adoption.adopter
GROUP BY user.id
id CONCAT(lastname, firstname) year month date
16 Mr.A 2016 6 25
17 Mr.B 2016 6 18
18 Mr.C NULL NULL NULL
19 Mr.D NULL NULL NULL
//after, I add this, should return Mr.A, C, D
HAVING SUM(CONCAT(year,month,date)= 2016618 ) = 0
我有一个查询LEFT JOIN两个表用户信息表&他们的日程表
我试图找出2016618上没有时间表的人
我使用了HAVING SUM,但它返回0行
有谁知道问题在哪里?
答案 0 :(得分:0)
我在我的本地服务器上尝试并且@Jhecht是对的,CONCAT返回字符串,你必须在2016618上添加引用
所以你的代码必须是
SELECT user.id, CONCAT(lastname, firstname), year, month, date FROM user
LEFT JOIN adoption ON user.id = adoption.adopter
GROUP BY user.id
HAVING SUM(CONCAT(year,month,date) = '2016618') = 0
答案 1 :(得分:0)
我只是尝试一下你的结果,也许对你有帮助。
CREATE TABLE Table1
(`id` int, `name` varchar(4), `year` varchar(4), `month` varchar(4), `date` varchar(4))
;
INSERT INTO Table1
(`id`, `name`, `year`, `month`, `date`)
VALUES
(16, 'Mr.A', '2016', '6', '25'),
(17, 'Mr.B', '2016', '6', '18'),
(18, 'Mr.C', NULL, NULL, NULL),
(19, 'Mr.D', NULL, NULL, NULL)
;
SELECT * FROM Table1
GROUP BY id
HAVING SUM(if(CONCAT(year,month,date) = 2016618, 1, 0)) = 0
结果:
| id | name | year | month | date |
+----+------+------+-------+------+
| 16 | Mr.A | 2016 | 6 | 25 |
| 18 | Mr.C | null | null | null |
| 19 | Mr.D | null | null | null |