我有这两个表
facilities
+----+--------------------------------------+--------------+-------+------+------------+------------+
| id | name | category | image | cust | created_at | updated_at |
+----+--------------------------------------+--------------+-------+------+------------+------------+
| 1 | Bar | hotel | | | NULL | NULL |
| 2 | Minibar in alle Zimmer | hotel | | | NULL | NULL |
| 3 | Snack restaurant | hotel | | | NULL | NULL |
| 4 | Grillroom | hotel | | | NULL | NULL |
+----+--------------------------------------+--------------+-------+------+------------+------------+
表 - 2
resortfacilities
+----+------+---------------------+---------------------+------+
| id | f_id | created_at | updated_at | h_id |
+----+------+---------------------+---------------------+------+
| 42 | 4 | 2016-09-21 13:17:27 | 2016-09-21 13:17:27 | 35 |
| 59 | 1 | 2016-09-22 10:23:27 | 2016-09-22 10:23:27 | 38 |
| 60 | 4 | NULL | NULL | 38 |
+----+------+---------------------+---------------------+------+
我在sql
之后运行了这个SELECT
facilities.id,
facilities.`name`,
facilities.image,
resortfacilities.h_id
FROM
facilities
LEFT JOIN resortfacilities ON facilities.id = resortfacilities.f_id
WHERE
resortfacilities.f_id IS NULL AND
facilities.category = 'hotel' OR
resortfacilities.h_id <> 35
期望得到以下结果
+----+---------------------------+-------+------+
| id | name | image | h_id |
+----+---------------------------+-------+------+
| 4 | Grillroom | | 38 |
| 2 | Minibar in alle Zimmer | | NULL |
| 3 | Snack restaurant | | NULL |
+----+---------------------------+-------+------+
没有属于h_id 35
的值,但我最终得到了
+----+---------------------------+-------+------+
| id | name | image | h_id |
+----+---------------------------+-------+------+
| 1 | Bar | | 38 |
| 4 | Grillroom | | 38 |
| 2 | Minibar in alle Zimmer | | NULL |
| 3 | Snack restaurant | | NULL |
+----+---------------------------+-------+------+
我可以知道如何获得该结果吗?
答案 0 :(得分:0)
使用以下脚本(用于获取上述结果)。
SELECT
f.id,
f.`name`,
f.image,
r.h_id
FROM
facilities f
LEFT JOIN resortfacilities r ON f.id = f.f_id AND r.h_id <> 35
WHERE f.category = 'hotel'