我有表'文章'
+-------------+ | articles | +----+--------+ | id | title | +----+--------+ | 1 | title1 | +----+--------+ | 2 | title2 | +----+--------+ | 3 | title3 | +----+--------+
表'目录'
+---------------------+ | catalogue | +----+--------+-------+ | id | group | name | +----+--------+-------+ | 1 | group1 | name1 | +----+--------+-------+ | 2 | group1 | name2 | +----+--------+-------+ | 3 | group2 | name3 | +----+--------+-------+ | 4 | group2 | name4 | +----+--------+-------+
绑定表'bindTable'
+------------+--------------+-------+ | bindTable | +------------+--------------+-------+ | id_article | id_catalogue | value | +------------+--------------+-------+ | 1 | 2 | 1 | +------------+--------------+-------+ | 1 | 3 | 4 | +------------+--------------+-------+ | 3 | 1 | 2 | +------------+--------------+-------+ | 3 | 3 | 1 | +------------+--------------+-------+ | 3 | 4 | 3 | +------------+--------------+-------+
我需要获得结果,如表'结果',我可以从表'article'获得所选项目的“catalogue_name:value”对
+-----------------------------------------------------+ | result | +------------+---------------+----------------+-------+ | article_id | article_title | catalogue_name | value | +------------+---------------+----------------+-------+ | 1 | title1 | group1_name2, | 1 | | | | group2_name3 | 4 | +------------+---------------+----------------+-------+ | 3 | title3 | group1_name1, | 2 | | | | group2_name3, | 1 | | | | group2_name4 | 3 | +------------+---------------+----------------+-------+
有人能用一个数据库查询告诉我一个查询字符串吗?谢谢你的关注。
My vision: SELECT b.id_article, a.title, c.group, c.name, b.value FROM bindTable b JOIN articles a ON a.id = b.id_articles JOIN catalogue c ON c.id = b.id_catalogue WHERE b.id_article = 1
但是对于一个a.id
,我需要一对c.name& b.value对答案 0 :(得分:0)
具有内部联接的选择
select a.article_id, a.article_title, b.catalogue_name, b.value
from bindTable as c
inner join articles as a on a.id = c. article_id
inner join catalogues as b on c. id_catalogue = b.id
答案 1 :(得分:0)
这应该这样做。
SELECT articles.id AS article_id,
articles.title AS article_title,
CONCAT_WS('_', catalogue.group, catalogue.name) AS catalogue_name,
bindTable.value AS value
FROM bindTable
INNER JOIN articles ON bindTable.id_article = articles.id
INNER JOIN catalogue ON bindTable.id_catalogue = catalogue.id