[英]How to select a column with max value of other column, and all corresponding rows of column in another table?
兩個給定的表:
問題:Select 費用最高的活動以及其中注冊的學生姓名。
結果應該是:活動欄(高爾夫)和學生欄(馬克安東尼)。 在這種情況下,只有一個學生注冊,但如果有更多學生注冊,我想說明一個案例。
我嘗試了各種解決方案,但似乎無法正確解決。
任何提示表示贊賞,謝謝。
編輯:我看到我被否決了,我不想展示我嘗試過的東西,因為我認為它離題了,但這里有一些:
SELECT s.Student, a.Activity from Activities as a inner join Students as s
ON a.ID = s.ID where a.Cost = (select max(a.Cost))
SELECT s.Student, a.cost, a.Activity from Activities as a inner join Students `as s ON a.ID = s.ID`
group by s.Student having a.cost = max(a.cost)
以下查詢有效。
CREATE TABLE activity ( `ID` INTEGER, `Activity` VARCHAR(8), `Cost` INTEGER ); INSERT INTO activity (`ID`, `Activity`, `Cost`) VALUES ('84', 'Swimming', '17'), ('84', 'Tennis', '36'), ('100', 'Squash', '40'), ('100', 'Swimming', '17'), ('182', 'Tennis', '36'), ('219', 'Golf', '47'), ('219', 'Swimming', '15'), ('219', 'Squash', '40');
CREATE TABLE students ( `Student` VARCHAR(11), `ID` INTEGER ); INSERT INTO students (`Student`, `ID`) VALUES ('John Smith', '84'), ('Jane Bloggs', '100'), ('John Smith', '182'), ('Mark Antor', '219');
SELECT Student,Activity FROM students s INNER JOIN ( SELECT id,Activity FROm activity WHERE Cost = (SELECT MAX(Cost) FROM activity)) a ON s.ID = a.id
學生 | 活動:--------- |:-------- 馬克安托 | 高爾夫球
db<> 在這里擺弄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.