我们正在尝试从MySQL数据库中的几个表中进行选择。
我们的查询是:
SELECT r.reviews_id,
r.customers_name,
r.date_added,
rd.reviews_text,
r.reviews_rating,
c.customers_email_address
FROM reviews r, reviews_description rd, customers c
WHERE r.customers_id = c.customers_id
AND r.reviews_id = rd.reviews_id
AND r.products_id = '74'
AND r.approved='1'
ORDER BY LENGTH(rd.reviews_text) DESC
但是,表reviews
在字段customers_id
命令WHERE r.customers_id = c.customers_id
仅将其限制为具有customers_id
我想知道我们如何选择NULL字段?
我们尝试了WHERE (r.customers_id = c.customers_id) OR (r.customers_id IS NULL)
- 但这不起作用。
提前感谢您的帮助。
答案 0 :(得分:2)
不要使用旧的隐式连接语法。使用显式连接 - 在本例中为left join
select r.reviews_id, r.customers_name, r.date_added, rd.reviews_text, r.reviews_rating,
c.customers_email_address
FROM reviews r
INNER JOIN reviews_description rd ON r.reviews_id = rd.reviews_id
LEFT JOIN customers c ON r.customers_id = c.customers_id
WHERE r.products_id = '74'
AND r.approved='1'
ORDER BY LENGTH(rd.reviews_text) DESC
答案 1 :(得分:-2)
应该是这样的:
WHERE (r.customers_id = c.customers_id = 0 OR r.customers_id IS NULL)
而不是:
WHERE (r.customers_id = c.customers_id) OR (r.customers_id IS NULL)