我有两个名为
的表
1.)Primary_Table
2.)Secondary_Table
如下图所示: -
Primary_Table Secondary_Table
Sl_no Sub_id Destination Sub_id Price
1 1000 New York 1000 1987
1 1001 Tokyo 1001 5679
1 1002 London 1002 7875
2 1003 Mumbai 1003 6789
2 1004 Sydney 1004 7489
2 1005 Munich 1005 6746
这里我正在尝试编写一个SQL / MYSQL查询来查找与主表中的Sl_no相关的最高和最低价格。
答案 0 :(得分:0)
您可以使用此解决方案。
SELECT
tb1.sub_id
, tb1.sl_no
, tb1.destination
, tb2.price
FROM
tb1
JOIN
tb2
ON
tb2.sub_id = tb1.sub_id
WHERE
price = (SELECT MIN(price) FROM tb2) OR price = (SELECT MAX(price) FROM tb2)
希望这能解决您的问题。
答案 1 :(得分:0)
此查询将为您提供每个Sub_id
SELECT Sub_id,MIN(Price) AS minimum, MAX(Price) AS maximum FROM
(SELECT p.si,p.sub,s.price FROM Primary_Table as p JOIN Secondary_Table as s ON
p.Sub_id=s.Sub_id) AS t GROUP BY t.Sub_id