如何创建一个选择其他表以查看是否有数据的select语句?
例如,假设我有一个充满城市和州的城市表,然后我有3个其他表填充了同时拥有城市和州的企业。那么我如何只获得有数据的城市?
例如,让我们来佛罗里达州。我如何让佛罗里达州拥有至少3家企业的城市?
我认为它会像
Select City C from Cities
where( Select (count) from FloridaBeds where City = C.City where (count) > 3
答案 0 :(得分:0)
要找到佛罗里达州有1-3张床位且堪萨斯州不到3张床位的城市,请尝试:
select c.City
from Cities c
join FloridaBeds fb
on fb.City = c.City
join KansasBeds kb
on kb.City = c.City
group by
c.City
having count(distinct fb.ID) between 1 and 3
and count(distinct kb.ID) <3
答案 1 :(得分:0)
要获得佛罗里达州至少有3家企业的城市列表,您可以这样做:
SELECT City
FROM Cities c
WHERE City IN
(
SELECT City
FROM FloridaBeds
GROUP BY City
HAVING COUNT(City) >= 3
)
城市名称似乎在您的FloridaBeds表格中。如果这是真的,并且您没有备用密钥,例如CityID,则可以执行此操作以获取城市名称列表。
SELECT City
FROM FloridaBeds
GROUP BY City
HAVING COUNT(City) >= 3
答案 2 :(得分:0)
要选择有数据的城市,请尝试以下方法:
SELECT DISTINCT City
FROM Cities RIGHT JOIN
FloridaBeds ON FloridaBeds.CityId = Cities.CityId