2014-11-06 205 views
1

的主題無關的問題我有兩個表:MySQL的 - 並行合併兩個具有相同的#行

exam_outline_items:

Query Table One

jml_quiz_pool:

Query Table Two

在所有的th我試着è的東西,這讓我最接近:

select t1.sequence, t1.title, t2.q_cat, t2.q_count 
from student_pl.exam_outline_items t1 
cross join pe_joomla.jml_quiz_pool t2 
where t1.exam_outline_id = 5 and t1.chapter_num > 0 
    and t2.q_id = 1109 and t2.q_count > 0 
group by title 

將會產生這樣的結果:

Query Results from 3rd query

我只需要那些q_cat值是不同的,就像他們在第2查詢。

在此先感謝您的幫助。

+0

你如何確定,哪個序列和標題屬於哪個q_cat和q_count? – fancyPants 2014-11-06 18:02:00

+0

只要查詢按照所述的方式運行,它們就可以完美匹配。 – jiy 2014-11-06 18:04:01

回答

3

你必須有一些東西來連接它們。如果你沒有這樣的列,你可以通過創建一個帶有變量的rownumber來模擬一個列。

select sequence, title, q_cat, q_count from (
    select t1.sequence, t1.title, @r1 := @r1 + 1 as rownumber 
    from student_pl.exam_outline_items t1 
    , (select @r1 := 0) var_init 
    where t1.exam_outline_id = 5 and t1.chapter_num > 0 
    order by t1.sequence 
) a 
inner join 
(
    select t2.q_cat, t2.q_count, @r2 := @r2 + 1 as rownumber 
    from pe_joomla.jml_quiz_pool t2 
    , (select @r2 := 0) var_init 
    where t2.q_id = 1109 and t2.q_count > 0 
    order by t2.q_cat 
) b on a.rownumber = b.rownumber; 

還要注意,我在這些查詢使用order by。在數據庫中,除非您明確將其設置爲order by,否則您沒有排序順序。

+0

非常感謝 - 完美的作品! – jiy 2014-11-06 18:20:42

相關問題