开发者

MYSQL join 2 tables

开发者 https://www.devze.com 2023-01-19 08:35 出处:网络
TABLE1 ID | NAME 1| a 2| b 3| c 4| d TABLE2 ID | TBL1_ID | NAME 1| 2| x 2| 2| y 3| 2| z I would like to join two tables to get 开发者_StackOverflow社区all records from table 1 and all records jo

TABLE1

ID | NAME
1  | a   
2  | b  
3  | c  
4  | d  

TABLE2

ID | TBL1_ID | NAME  
1  | 2       | x     
2  | 2       | y  
3  | 2       | z  

I would like to join two tables to get 开发者_StackOverflow社区all records from table 1 and all records joined from table 2 on ID

This query return only all rows from 1 table and 1 row from second table.

SELECT  a.*, COUNT(a.id) total FROM table1 a  
  LEFT JOIN table2 b ON a.id = b.tbl1_id    
  GROUP BY a.id  
  ORDER BY a.id DESC  

Thanks.


SELECT  * total FROM table1 a  
  left JOIN table2 b ON a.id = b.tbl1_id   


If You need to join both tables and show all fields on matching rows this should work:

SELECT  * FROM table1 a  
  LEFT JOIN table2 b ON(a.id = b.tbl1_id)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号