MySQL连接问题
我正在尝试连接3个表,以某种方式进行分层内部连接,并从第3个表中获取数据。我的起点是article表中的article_编号(156118)。下面是正在工作的sql语句和表结构,但必须有一种方法将所有这些连接在一起,对吗MySQL连接问题,sql,mysql,Sql,Mysql,我正在尝试连接3个表,以某种方式进行分层内部连接,并从第3个表中获取数据。我的起点是article表中的article_编号(156118)。下面是正在工作的sql语句和表结构,但必须有一种方法将所有这些连接在一起,对吗 // Get the parent task from the article select task_parent from article a, tasks t where a.task_id = t.task_id and a.article_number = 1561
// Get the parent task from the article
select task_parent
from article a, tasks t
where a.task_id = t.task_id
and a.article_number = 156118
// Get the task id for the 'Blog' task
select task_id
from tasks
where task_parent = 26093
and task_name like '%blog%'
// Get ALL the blog record
select *
from blogs
where task_id = 26091
---------Tables------------
* article table *
id | article_number | task_id
1 | 156118 | 26089
* tasks table *
id | task_name | task_parent
26089 | article | 26093
26091 | blogs | 26093
26093 | Main Task | 26093
* blog table *
id | task_id | content
1 | 102 | blah
2 | 102 | blah
3 | 102 | blah
-------------
*如何使用一条SQl语句仅使用文章编号获取所有博客数据
提前谢谢 编辑:重读问题后修改答案。您需要两个到任务表的联接。一个用于获取文章任务的父级,另一个用于获取与文章任务具有相同父级的博客任务
select b.id, b.task_id, b.content
from article a
inner join tasks t1
on a.task_id = t1.task_id
inner join tasks t2
on t1.task_parent = t2.task_parent
and t2.task_name like '%blog%'
inner join blogs b
on t2.task_id = b.task_id
where a.article_number = 156118
看起来你想把它们都绑在一起,只需使用商品编号作为参数。。。 尝试: 给你
SELECT * FROM a
INNER JOIN tasks USING (a.article_number)
INNER JOIN blogs USING (a.article_number)
WHERE a.article_number = 156118;
完美的乔…这正是我想要的,而且它运行正常。谢谢
SELECT * FROM a
INNER JOIN tasks USING (a.article_number)
INNER JOIN blogs USING (a.article_number)
WHERE a.article_number = 156118;