MYSQL 加入 2 table 与 2 个关系 table
MYSQL Join 2 tables with 2 relations table
您好,我需要从数据库中获取数据,但我不知道该怎么做...
它们有 "boutiques" table 和 "boutiques_categories" 以及它们之间的关系 table name "boutiques_categories_categories"
这里是它们的设置方式:
boutiques
id name
1 X_boutiques2_name
2 Y_boutiques_name
Boutique2
id name
1 X_boutiques2_name
2 Y_boutiques2_name
boutiques_categories
id name
1 X_categorie_name
2 Y_categorie_name
boutiques_categories_categories
boutique_id categorie_id
X_boutique_id X_categorie_id
Y_boutique_id Y_categorie_id
我想创建一个 SQL 来合并和打印数据的 CSV,如下所示:
export
boutiques_id boutique_name boutiques_categories_categorie_name
boutiques2_id boutique2_name boutiques_categories_categorie_name
我正在尝试 UNION 那些 2 table
SELECT A.*
FROM boutiques A
UNION select B.* FROM boutiques2 B
它正在运行,但现在我需要将类别名称加入其中,但我不知道该怎么做。试过:
SELECT A.*
FROM boutiques A
UNION select B.*
FROM boutiques2 B
left join
boutiques_categories BC ON BC.id =
(
SELECT BCC.categorie_id
FORM
boutiques_categories_categories BCC
WHERE BCC.boutique_id = BC.id
)
但我收到无法解决的 MYSQL 错误。
#1242 - Subquery returns more than 1 row
感谢您的宝贵时间。
您可以将 boutiques
和 Boutique2
合并(或 union all
,具体取决于您想要的数据)到派生的 table,然后将其连接到 boutiques_categories 通过 boutiques_categories_categories.
您在问题中包含的 table 数据似乎并不完全准确。具体来说:boutiques_categories_categories 包含 boutique_id 和 categorie_id 值,这些值与关联的 table 的 id 列不匹配。我只是假设 boutiques_categories_categories 具有 int ID 值。如果不是这种情况,请在评论中说明,我可以适当调整查询。
试试这个查询,看看它 returns 是否是您期望的数据:
select
b.id, b.name, c.name
from
-- Get the boutiques rows from the unioned tables
(
select id, name from boutiques
union
select id, name from Boutique2
) as b
-- Join in boutiques_cateogires_categories
join boutiques_categories_categories cc
on (b.id=cc.boutique_id)
-- Join in boutiques_categories
join boutiques_categories c
on (cc.categorie_id=c.id)
您好,我需要从数据库中获取数据,但我不知道该怎么做...
它们有 "boutiques" table 和 "boutiques_categories" 以及它们之间的关系 table name "boutiques_categories_categories"
这里是它们的设置方式:
boutiques
id name
1 X_boutiques2_name
2 Y_boutiques_name
Boutique2
id name
1 X_boutiques2_name
2 Y_boutiques2_name
boutiques_categories
id name
1 X_categorie_name
2 Y_categorie_name
boutiques_categories_categories
boutique_id categorie_id
X_boutique_id X_categorie_id
Y_boutique_id Y_categorie_id
我想创建一个 SQL 来合并和打印数据的 CSV,如下所示:
export
boutiques_id boutique_name boutiques_categories_categorie_name
boutiques2_id boutique2_name boutiques_categories_categorie_name
我正在尝试 UNION 那些 2 table
SELECT A.*
FROM boutiques A
UNION select B.* FROM boutiques2 B
它正在运行,但现在我需要将类别名称加入其中,但我不知道该怎么做。试过:
SELECT A.*
FROM boutiques A
UNION select B.*
FROM boutiques2 B
left join
boutiques_categories BC ON BC.id =
(
SELECT BCC.categorie_id
FORM
boutiques_categories_categories BCC
WHERE BCC.boutique_id = BC.id
)
但我收到无法解决的 MYSQL 错误。
#1242 - Subquery returns more than 1 row
感谢您的宝贵时间。
您可以将 boutiques
和 Boutique2
合并(或 union all
,具体取决于您想要的数据)到派生的 table,然后将其连接到 boutiques_categories 通过 boutiques_categories_categories.
您在问题中包含的 table 数据似乎并不完全准确。具体来说:boutiques_categories_categories 包含 boutique_id 和 categorie_id 值,这些值与关联的 table 的 id 列不匹配。我只是假设 boutiques_categories_categories 具有 int ID 值。如果不是这种情况,请在评论中说明,我可以适当调整查询。
试试这个查询,看看它 returns 是否是您期望的数据:
select
b.id, b.name, c.name
from
-- Get the boutiques rows from the unioned tables
(
select id, name from boutiques
union
select id, name from Boutique2
) as b
-- Join in boutiques_cateogires_categories
join boutiques_categories_categories cc
on (b.id=cc.boutique_id)
-- Join in boutiques_categories
join boutiques_categories c
on (cc.categorie_id=c.id)