2014-11-06 139 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