问题是这个。。
表是这样的。。
+--------------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------+------+-----+---------+----------------+
| facility_map_id | int(10) | NO | PRI | NULL | auto_increment |
| facility_map_facility_id | int(10) | NO | MUL | NULL | |
| facility_map_listing_id | int(10) | NO | | NULL | |
+--------------------------+---------+------+-----+---------+----------------+
数据是这样的。。
+-----------------+--------------------------+-------------------------+
| facility_map_id | facility_map_facility_id | facility_map_listing_id |
+-----------------+--------------------------+-------------------------+
| 248 | 1 | 18 |
| 259 | 1 | 19 |
| 206 | 1 | 20 |
| 244 | 1 | 21 |
| 249 | 2 | 18 |
| 207 | 2 | 20 |
| 208 | 3 | 20 |
| 245 | 3 | 21 |
| 260 | 4 | 19 |
| 261 | 5 | 19 |
| 246 | 6 | 21 |
| 250 | 7 | 18 |
| 247 | 8 | 21 |
+-----------------+--------------------------+-------------------------+
我运行这个查询:
SELECT facility_map_listing_id
FROM facility_map
WHERE facility_map_facility_id IN(1, 2)
GROUP BY facility_map_listing_id
HAVING count(DISTINCT facility_map_facility_id) >= 2
得到这个..
+-------------------------+
| facility_map_listing_id |
+-------------------------+
| 18 |
| 20 |
+-------------------------+
2 rows in set (0.00 sec)
哪个是对的!- 但谁能解释一下,为什么 GROUP BY 需要在语句中?
如果它不是并且我运行相同的查询而忽略了我得到的 GROUP BY..
+-------------------------+
| facility_map_listing_id |
+-------------------------+
| 18 |
+-------------------------+
1 row in set (0.00 sec)
谁能给我解释一下?谢谢!