【问题标题】:select the min value of a id from other table从其他表中选择一个 id 的最小值
【发布时间】:2022-01-18 21:22:53
【问题描述】:

我想从其他表中选择一个id的最小值

这是我的桌子1

id grades
1 1
2 2
1 3
1 4
2 5

我的桌子2

id name
1 andy
2 lucy
3 kevin

我试过了

select table2.id, name, min(table1.grades) as grade from table1, table2

但它只显示 1 条记录

我的预期

id name grade
1 andy 1
2 lucy 2
3 kevin 0(or null?)

谢谢

【问题讨论】:

    标签: mysql sql min


    【解决方案1】:
    SELECT id, table2.name, MIN(table1.grades) grade
    FROM table1
    JOIN table2 USING (id)
    GROUP BY 1,2;
    

    【讨论】:

    • 你快了 15 秒!
    • @SlavaRozhnev 我试过了...
    • 如果 table2 上有 id "3" 但 table1 上没有成绩,则不显示任何内容
    • @leo,使用left join
    • @leo .. FROM table2 LEFT JOIN table1 USING (id) ..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多