【问题标题】:MySQL merge data from multiple tablesMySQL合并来自多个表的数据
【发布时间】:2016-04-29 05:46:41
【问题描述】:

我有几个表格,一个包含主要内容的表格,以及一些使用子类创建的表格。

这个项目在 MySQL 中

父表

===========================
| id       | name         |
---------------------------    
| 1        | website 1    |
| 2        | website 2    | 
| 3        | website 3    |
===========================

child_table_1(例如烹饪网站)

=========================================================
| id       | fk   | url             | books_for_sale    |
---------------------------------------------------------    
| 1        | 1    | http://foo.com  | Cooking food      |
| 2        | 3    | http://bar.com  | Cooding bars      |
=========================================================

child_table_2(例如游戏网站)

========================================================
| id       | fk   |url             | games_for_sale    |
--------------------------------------------------   
| 1        | 2    |http://dad.com  | Daddy Daycare     |
========================================================

现在我需要将所有表中的信息合并为一个。

========================================================================
| id       | fk   |url             | books_for_sale    | games_for_sale|
------------------------------------------------------------------------
| 1        | 1    |http://foo.com  | Cooking food      |               |
| 2        | 2    |http://dad.com  |                   | Daddy Daycare |
| 3        | 3    |http://bar.com  | Cooding bars      |               |  
========================================================================

我试过了:

SELECT * FROM parent_table
LEFT JOIN child_table_1
ON parent_table.id = child_table_1.id
LEFT JOIN child_table_2
ON parent_table.id = child_table_2.id

还有这个:

SELECT * FROM parent_table
LEFT JOIN child_table_1
ON parent_table.id = child_table_1.id
LEFT JOIN child_table_2
ON child_table_1.id = child_table_2.id

但它们根本不起作用。
我得到的只是连接中最后一个表的内容。

在实际情况下,我需要处理大约十几个包含不同列的表。

我希望有人可以在这里帮助我。 如果有人知道在 Django 中执行此操作的好方法,那就更好了。

提前致谢。

【问题讨论】:

  • 你期待什么样的结果?第三个表有 3 行?
  • @SimonHi 是的,我想要最后一张包含所有列的表格
  • 结合 JOIN 和 UNION(查看我的更新)

标签: mysql django python-2.7


【解决方案1】:

您似乎正在尝试使用每个表的主键来连接表,而不是您应该使用外键,这样您就可以建立一对多的关系。

id 列标识该特定表的每一行,您需要一个 websiteid 列作为 urls 表中的外键,这样您就可以将外键与主键匹配。

从表 1 中选择 * 在 table2.fkid = table1.id 上加入 table2

另一个可行的选择是联合

从父表中选择 * 左连接 child_table_1 ON parent_table.id = child_table_1.fkid 联合所有 从父表中选择 * 左连接 child_table_2 ON parent_table.id = child_table_2.fkid

您还必须确保两个 select 语句包含相同的列

【讨论】:

  • 我已将外键添加到表中。
  • 您可能需要使用联合,同样在您的示例查询中,您正在加入主键,我将进行编辑以演示。
【解决方案2】:

精确生成问题表的解决方案(结合 JOIN 和 UNION,使用 NULL 作为 X 来表示缺失的列)

SELECT parent_table.id as id,fk,url,books_for_sale,NULL as games_for_sale
FROM parent_table JOIN child_table_1 ON parent_table.id = child_table_1.fk
UNION
SELECT parent_table.id as id,fk,url,NULL as books_for_sale,games_for_sale
FROM parent_table JOIN child_table_2 ON parent_table.id = child_table_2.fk
ORDER BY id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多