如果不存在零值,则SQL记录仅返回

时间:2016-12-06 22:08:54

标签: mysql sql database join

这是我的MySQL数据库中的注册表。

SIGNUP
id (int)
customer (int)
helper (int)
eid (int)
..plus more fields

当我希望它返回数据库中的记录时,此查询返回0结果。情况是SIGNUP表有一条记录,其中customer = 11且helper = 0且approved = 0

如果辅助字段填充的数字不为0,则返回记录,但如果为0,则返回0条记录。如果我取出第二个选择行(包含v.field1,v.field2等等),那么它也会起作用。问题似乎是没有数据可以从帮助列中提取,因此它不允许返回任何其他内容,但仍有数据我想为其他字段返回。开放的想法和所有帮助表示赞赏!非常感谢..

SELECT 
    u.id cId, u.first cFirst, u.email cEmail, u.username cUsername,   
    v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, 
    s.customer, s.helper, s.eid EID, s.approved, 
    e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip
FROM signup s 
INNER JOIN events e ON e.id = s.eid
INNER JOIN users u ON u.id = s.customer
INNER JOIN users v ON v.id = s.helper
WHERE 
    (s.customer = 11 OR s.helper = 11)
AND e.date > sysdate()
ORDER BY e.date asc

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要SELECT u.id cId, u.first cFirst, u.email cEmail, u.username cUsername, v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, s.customer, s.helper, s.eid EID, s.approved, e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip FROM signup s INNER JOIN events e ON e.id = s.eid INNER JOIN users u ON u.id = s.customer LEFT JOIN users v ON v.id = s.helper WHERE (s.customer = 11 OR s.helper = 11) AND e.date > sysdate() ORDER BY e.date asc 。这样即使没有匹配的帮助器,您也可以获得所有行。

SELECT 
    u.id cId, u.first cFirst, u.email cEmail, u.username cUsername,   
    v.id hId, v.first hFirst, v.email hEmail, v.username hUsername, 
    s.customer, s.helper, s.eid EID, s.approved, 
    e.name, DATE_FORMAT(e.date, '%W, %b %e %Y %l:%i %p') date, e.summary, e.street, e.city, e.state, e.zip eZip
FROM signup s 
INNER JOIN events e ON e.id = s.eid
INNER JOIN users u ON u.id = s.customer
left JOIN users v ON v.id = s.helper
WHERE (s.customer = 11 OR s.helper = 11)
AND e.date > sysdate()
ORDER BY e.date asc

答案 1 :(得分:1)

请改为尝试:

bdh()