表"服务器":
+----+----------------+
| id | disabled_until |
+----+----------------+
| 1 | 0 | // disabled_until is an epoch timestamp
| 2 | 146893210 | // (irrelevant)
| 3 | 1468969442 |
+----+----------------+
表" server_disables":
+----+-----------+-------+----------------+
| id | server_id | tld | disabled_until |
+----+-----------+-------+----------------+
| 1 | 1 | .com | 0 |
| 2 | 1 | .org | 1468969900 |
| 2 | 3 | .com | 1468969900 |
+----+-----------+-------+----------------+
我正在尝试进行查询,从服务器中选择行:
我相信我已经创建了一个可以完成#2而不是#1的查询。在server_disables中有0个匹配行时,我不知道在哪里继续或如何添加案例。
你可以在OR d.disabled_until IS NULL
看到我的尝试,但它不起作用。
查询:
SELECT s.* FROM servers s
INNER JOIN server_disables d ON (d.server_id = s.id AND d.tld = '.com')
WHERE (s.disabled_until < 1468984373)
AND (d.disabled_until < 1468984373 OR d.disabled_until IS NULL)
ORDER BY RAND() LIMIT 5
我的问题是如何使我的查询也显示来自&#34;服务器&#34;的行。从INNER JOIN
合并0行的表格。
答案 0 :(得分:2)
你应该使用LEFT JOIN
,也许有重复的行,所以你可能还需要使用DISTINCT
,试试这个:
SELECT DISTINCT s.* FROM servers s
LEFT JOIN server_disables d ON (d.server_id = s.id AND d.tld = '.com')
WHERE (s.disabled_until < 1468984373)
AND (d.disabled_until IS NULL OR d.disabled_until < 1468984373)
ORDER BY RAND() LIMIT 5