我有一些 table
tech_map(17 行): id,name,status, id_user_add, datetime_add, commentary
tech_map_expenses(7 行): id,name,cost, tech_map.id
tech_map_products(8 行): id、category_id、catalog_id、total、tech_map_id
tech_map_stock(8 行): id、category_id、catalog_id、total、tech_map_id
我想选择退出与其他表的连接的 tech_map 例如:
SELECT tech_map.*
FROM
`tech_map`
INNER JOIN tech_map_expenses ON tech_map_expenses.tech_map_id = tech_map.id
INNER JOIN tech_map_products ON tech_map_products.tech_map_id = tech_map.id
INNER JOIN tech_map_stock ON tech_map_stock.tech_map_id = tech_map.id
ORDER BY tech_map.id DESC
但它返回了太多行 (33) 和重复记录。
最佳答案
您应该将 Left Join 更改为 Inner Join。为什么?
Left Join 将只包含所有没有 与其他表的连接。
Inner Join 将只包含所有有连接的 tech_map 与其他表。
看看这个picture ,它总是对我有帮助。
关于mysql - 一对多连接,多张表sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070314/