duanbi9202 2017-09-25 12:21
浏览 50

从两个具有id的表中检索数据,并在Mysql中显示另一个表的最大id

I have a two different tables

Table 1
id
name
description

Table 2
id 
details
info
table1_id

I want to display all the records from the table1 except id but from table2 I used to display the max id.

eg. table1 have following records

id=1
name = test
description = some text

table2 have
id=5
details = some more text
info = the new info
table1_id = 1

so the result what I want is

id  name   description
5   test   some text
  • 写回答

2条回答 默认 最新

  • douhan4812 2017-09-25 12:25
    关注

    Try this:

    select
        (select max(table2.id) from table2 where table1.id = table2.table1_id) id,
        name,
        description
    from table1
    

    or left join:

    select
        t.id,
        table1.name,
        table1.description
    from table1
    left join (
        select max(id) id, table1_id from table2 group by table1_id
    ) t on table1.id = t.table1_id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题