English 中文(简体)
MySQL加入问题
原标题:MySQL join question
  • 时间:2011-01-20 17:01:24
  •  标签:
  • sql
  • mysql

我试图加入3个表格,有些是等级内的人加入,并从第3个表中获取数据。 我的出发点是,从该条表中删除第1条(156118)。 这里是工作说明和表格结构,但必须有一个办法,以正确的方式把所有这些内容结合起来?

//  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

-------------

<><>>> 我如何获得所有博客数据,用1份SQl声明只用第_条编号?

提前感谢!

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 重读后的回答。 你需要两人加入任务表。 一是让该条的母子承担任务,二是同第条任务相同的母子承担博客任务。

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
问题回答

Looks like you re wanting to tie them all together and just use the article number as the parameter... Try:


select b.* 
from blogs b, tasks t, tasks tp, article a  
where b.task_id = t.task_id 
and t.task_parent = tp.task_id 
and tp.task_id = a.task_id 
and a.article_number = 156118 

Here you go.

SELECT * FROM a
INNER JOIN tasks USING (a.article_number)
INNER JOIN blogs USING (a.article_number)
WHERE a.article_number = 156118;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签