情况是我需要选择一个与table1不同的字段(没有重复项),并使用结果作为键从另一个table2中进行选择。我需要在一个查询中。这可能吗?!
table1:hID,hName,hLocation table2:hID,hFrom,hTo,hRate,hRoomType,hMeals
我想更正此查询的版本:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
预期结果:所有提供双人间的酒店都非常感谢 -
感谢您的帮助!
答案 0 :(得分:0)
你的问题很模糊,令人困惑。这是你在找什么:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
答案 1 :(得分:0)
这是实现这一目标的骨架:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
另一种解决方案,如果您不想从table1中检索任何列:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
对于您的表格和问题
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)