Mysql 基于“将表连接到自身”;“连接”;桌子
我有两张桌子:Mysql 基于“将表连接到自身”;“连接”;桌子,mysql,Mysql,我有两张桌子: 项目表格: id title unique_flat_title --------------------------------------------- 1 post 1 post_1 2 post 2 post_2 3 category 1 category_1 4 post 3 post_3 5 post 4 post_4 6 categ
项目
表格:
id title unique_flat_title
---------------------------------------------
1 post 1 post_1
2 post 2 post_2
3 category 1 category_1
4 post 3 post_3
5 post 4 post_4
6 category 2 category_2
7 post 5 post_5
post_id category_id
----------------------------------
1 3
1 6
5 6
7 3
4 3
4 6
项目连接
表格:
id title unique_flat_title
---------------------------------------------
1 post 1 post_1
2 post 2 post_2
3 category 1 category_1
4 post 3 post_3
5 post 4 post_4
6 category 2 category_2
7 post 5 post_5
post_id category_id
----------------------------------
1 3
1 6
5 6
7 3
4 3
4 6
我想打印连接到另一个项目的所有项目,其中唯一\u平面\u标题
为类别\u 2
。可能吗?提前谢谢你的帮助
编辑:
假设我需要unique\u flat\u title
为category\u 1
的帖子列表
因此,我需要以下列表:
category_flat_title post_id post_title
------------------------------------------------------
category_1 1 post 1
category_1 7 post 5
category_1 4 post 3
您可以将“items”表连接到“items\u connection”,使用第一个表的ID与第二个表的CATGEORY\u ID匹配。
然后,您可以再次将其连接到“items”表,使用第二个的POST_ID与第三个的ID进行匹配。
它看起来是这样的:
SELECT A.unique_flat_title As category_flat_title
, C.id As post_id , C.unique_flat_title As post_title
from items A
join item_connection B on B.category_id = A.id
join items C on C.id=B.post_id
“连接到其他项目”是什么意思?除了打印类别2中的项目外,您还想做其他事情吗?这些项目是如何连接的<代码>项目。id等于
类别\u id
?假设我需要unique\u flat\u title
为category\u 1
的帖子列表。然后我需要以下列表作为结果:category\u flat\u title post\u id post\u title-------------------------------------category\u 1 post 1 category\u 1 7 post 5 category\u 1 4 post 3
编辑了我的原始帖子,很抱歉上面没有格式的评论。