2015-09-02 84 views
1

我試圖從兩個表中獲取日期,但問題是他們正在返回多個行的個人ID。 我有表像這樣: -從MySQL中的兩個表中檢索數據

Transaction 
----------- 
tid|startdate|enddate|status 
100|2-9-2015|2-9-2015|success 
200|1-9-2015|1-9-2015|failed 
300|31-8-2015|31-8-2015|success 

Test_Details 
------------ 
id|tid|test_name|test_status 
1|100|Test1|yes 
2|100|Tes2|no 
3|100|Test3|yes 
4|200|Test1|no 
5|200|Tes2|no 
6|200|Test3|yes 
7|300|Test1|yes 
8|300|Tes2|no 
9|300|Test3|no 

期望的結果: -

tid|startdate|enddate|status|Test1|Test2|Test3 
100|2-9-2015|2-9-2015|success|yes|no|yes 
200|1-9-2015|1-9-2015|failed|no|no|yes 
300|31-8-2015|31-8-2015|success|yes|no|no 

我使用這個查詢: -

select distinct ts.tid as tid, ts.startdate as startdate, 
ts.enddate as enddate, ts.status as status, 
CASE WHEN (td.test_name='Test1') THEN test_status END as Test1, 
CASE WHEN (td.test_name='Test2') THEN test_status END as Test2, 
CASE WHEN (td.test_name='Test3') THEN test_status END as Test3 
from transation ts, test_details td where ts.tid =td.id 

但是我卻越來越多行。 這裏是SQLFiddle演示

回答

0

您可以使用GROUP BY查詢,例如與GROUP_CONCAT聚合函數:

SELECT 
    ts.tid as tid, 
    ts.startdate as startdate, 
    ts.enddate as enddate, 
    ts.status as status, 
    GROUP_CONCAT(test_status ORDER BY td.test) 
FROM 
    transation ts LEFT JOIN test_details td ON ts.tid =td.id 
GROUP BY 
    ts.tid, 
    ts.startdate, 
    ts.enddate, 
    ts.status 

或此查詢,如果你想在不同的列狀態:

SELECT 
    ts.tid as tid, 
    ts.startdate as startdate, 
    ts.enddate as enddate, 
    ts.status as status, 
    MAX(CASE WHEN (td.test_name='Test1') THEN test_status END) as Test1, 
    MAX(CASE WHEN (td.test_name='Test2') THEN test_status END) as Test2, 
    MAX(CASE WHEN (td.test_name='Test3') THEN test_status END) as Test3 
FROM 
    transation ts LEFT JOIN test_details td ON ts.tid =td.id 
GROUP BY 
    ts.tid, 
    ts.startdate, 
    ts.enddate, 
    ts.status 
+0

喜添加組後用自己的查詢,我想第二個,但它佔用太多的時間執行。 –

+0

@NikhilGupta你有一個關於transaction.tid的索引嗎?和一個關於test_details.id?這應該會提高性能 – fthiella

1

只要test_details不是臨時表,您可以執行以下操作:

SELECT ts.tid, ts.startdate, ts.enddate, ts.status, 
    t1.test_status Test1, 
    t2.test_status Test2, 
    t3.test_status Test3 
FROM transaction ts 
    LEFT JOIN test_details t1 ON (t1.tid=ts.tid AND t1.test_name='Test1') 
    LEFT JOIN test_details t2 ON (t2.tid=ts.tid AND t2.test_name='Test2') 
    LEFT JOIN test_details t3 ON (t3.tid=ts.tid AND t3.test_name='Test3'); 

這是假設transaction.tid是唯一的,並且test_details中的(tid,test_name)對是唯一的。

編輯:在回答OP的評論「這是工作,但它是在大分貝慢」我建議添加索引到test_details.tidtest_details.test_name

ALTER TABLE test_details ADD INDEX (tid); 
ALTER TABLE test_details ADD INDEX (test_name); 

而且,如果所有的三個測試已知的所有交易存在,您可能需要使用,而不是JOINLEFT JOIN

SELECT ts.tid, ts.startdate, ts.enddate, ts.status, 
    t1.test_status Test1, 
    t2.test_status Test2, 
    t3.test_status Test3 
FROM transaction ts 
    JOIN test_details t1 ON (t1.tid=ts.tid AND t1.test_name='Test1') 
    JOIN test_details t2 ON (t2.tid=ts.tid AND t2.test_name='Test2') 
    JOIN test_details t3 ON (t3.tid=ts.tid AND t3.test_name='Test3'); 
+0

它的工作,但它在大分貝慢 –

0
select 
    ts.tid as tid, 
    ts.startdate as startdate, 
    ts.enddate as enddate, 
    ts.status as status, 
    group_concat(test1) test1, 
    group_concat(test2) test2, 
    group_concat(test3) test3 
    from 
    (select 
     ts.tid as tid, 
     ts.status as status, 
     CASE WHEN (td.test_name='Test1') THEN test_status END as Test1, 
     CASE WHEN (td.test_name='Tes2') THEN test_status END as Test2, 
     CASE WHEN (td.test_name='Test3') THEN test_status END as Test3 
     from 
     transation ts 
     join 
     test_details td 
     on ts.tid=td.tid 
    ) t 
group by tid 
0

您可以只用保留制度

select distinct ts.tid as tid, ts.startdate as startdate, 
ts.enddate as enddate, ts.status as status, 
CASE WHEN (td.test_name='Test1') THEN test_status END as Test1, 
CASE WHEN (td.test_name='Test2') THEN test_status END as Test2, 
CASE WHEN (td.test_name='Test3') THEN test_status END as Test3 
from transation ts, test_details td 
where ts.tid =td.id 
group by ts.tid; 
+0

嗨扎法爾,在這種情況下,我得到Test1的價值和其他人爲空 –

+0

你可以創建一個sqlfiddle來檢查它.... –