简体   繁体   English

MYSQL一个表中的所有记录,只有另一表中的最后一个连接的记录

[英]MYSQL All records from one table and only the last joined record from other table

I have the next two tables 我有接下来的两张桌子

Tabel Name 表格名称

id -  Name   
1  -  Jack   
2  -  Roger   
3  -  Peter    
4  -  Clark  
5  -  Gerard

and

Table numbers 表号

id  - name_id  -  number
1  -       1 -          10
2  -       1 -          20
3  -       2 -          30
4  -       2 -          40
5  -       3 -          50
6  -       3 -          60
7  -       4 -          70
8  -       4 -          80
9  -       5 -          90
10 -       5 -         100

Now in need the next outcome of a mysql query 现在需要mysql查询的下一个结果

name.id - name.name - numbers.id – numbers.number
  1     -   Jack    -      2     -       20 
  2     -   Roger   -      4     -       40
  3     -   Peter   -      6     -       60
  4     -   Clark   -      8     -       80
  5     -   Gerard  -      10    -      100

In short. 简而言之。 All the records of the name table and only the last join from the number table. 名称表的所有记录,以及编号表中的最后一个联接。

thnx n

Since you don't have a field where save the date, ill assume the last inserted item will have the higher id value. 由于您没有保存日期的字段,因此我假设最后插入的项目的ID值较高。

Check this out: sqlfiddle 检查一下: sqlfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM