mysql - 一对多连接,多张表sql

标签 mysql sql join

我有一些 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/

相关文章:

asp.net-mvc - 使用 Entity Framework 连接两个数据库中的表

MySQL JOIN 两个表查询

MySQL:如何在从另一个表获取的每个间隔之间从一个表中选择行

mysql - 有没有办法优化 MYSQL 中的 `SHOW DATABASES` 查询

sql合并两个具有不同布局MS-Access的表

sql - 具有多个 SET 的 PostgreSQL UPDATE

mysql - SQL 查询所有日期的分组截止日期的最大值

php - 在 PHP 中使用 while 和 foreach 循环

php - 存储/选择货币值(贷方/借方)mysql

php - Laravel 5 SQLSTATE [42S22] : Column not found