我有两张桌子。一个是:
+--------------------------+-------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+--------------------------+-------------+---------+-----+------+
| facility_id | int(10) | NO | PRI | NULL |
| facility_name | varchar(30) | | | NULL |
| facility_model | varchar(25) | | | NULL |
+--------------------------+-------------+------+-----+---------+
另一个是:
+--------------------------+-------------+------+-----+---------+
| Field | Type | Null | Key | Default |
+--------------------------+-------------+---------+-----+------+
| facility_id | int(10) | NO | PRI | NULL |
| facility_serial_number | varchar(30) | | | NULL |
+--------------------------+-------------+------+-----+---------+
不同的设施可以具有相同的facility_serial_number(这是我们想要找出并修复的)。我想获得其facility_serial_number不止一次出现的所有设施信息。
我尝试使用group by并拥有:
select f1.facility_id, f1.facility_name, f1.facility_model, f2.facility_serial_number
from f1, f2 on f1.facility_id = f2.facility_id
group by f2.facility_serial_number
having count (facility_serial_number) > 2
这是错的。我也尝试过其他方法,但没有成功。什么是corerct查询语法?
答案 0 :(得分:0)
你的内容与你的sql有什么关系并不明显,但是正确的syntac就像是
select f1.facility_id,
f1.facility_name,
f1.facility_model,
f2.facility_serial_number
from f1 INNER JOIN
f2 on f1.facility_id = f2.facility_id
group by f1.facility_id,
f1.facility_name,
f1.facility_model,
f2.facility_serial_number
having count (facility_serial_number) > 2
或
select f2.facility_serial_number
from f1 INNER JOIN
f2 on f1.facility_id = f2.facility_id
group by f2.facility_serial_number
having count (facility_serial_number) > 2
但如果我理解正确你可能想尝试类似
的东西select f1.facility_id,
f1.facility_name,
f1.facility_model,
f2.facility_serial_number
from f1 INNER JOIN
f2 on f1.facility_id = f2.facility_id
WHERE f2.facility_serial_number IN (
select f2.facility_serial_number
from f1 INNER JOIN
f2 on f1.facility_id = f2.facility_id
group by f2.facility_serial_number
having count (facility_serial_number) > 2
)
答案 1 :(得分:0)
尝试关注Ref
SELECT f1.facility_id, f1.facility_name, f1.facility_model, f2.facility_serial_number
FROM f1 LEFT JOIN f2 on f1.facility_id = f2.facility_id
GROUP BY f2.facility_serial_number
HAVING count (f2.facility_serial_number) > 2
答案 2 :(得分:0)
select * form f1 from where facility_id in(
select facility_id where f2 group by facility_id
having count(facility_serial_number) > 2))