在一个查询中组合多个sql / mysql查询

时间:2014-12-04 13:20:00

标签: php sql join

我有5张桌子。即userreference_1reference_2reference_3question

user => (user_id,user_name,user_emailid,user_mobno)
reference_1 => (ref_id_1,user_id,ref_name,ref_email,ref_mobno)
reference_2 => (ref_id_2,user_id,ref_name,ref_email,ref_mobno)
reference_3 => (ref_id_3,user_id,ref_name,ref_email,ref_mobno)
question => (que_id,user_id,ref_id_1,ref_id_2,ref_id_3,que1_name,que2_name,que3_name,que4_name,que5_name,que6a_name,que6b_name,que7_name,qa1,qa2,qa3,qa4,qa5,qa6a,qa6b,qa7,flag)

我连续4次查询:

查询1:使用LEFT JOIN从user表和question获取数据

select u.user_name,u.user_emailid,u.user_mobno,
  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from user u
LEFT JOIN question q
on u.user_id = q.user_id
WHERE q.flag = 1

查询2:使用LEFT JOIN从user表和reference_1获取数据

select r1.ref_name,r1.ref_email,r1.ref_mobno,
  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_1 r1
left join question q
on r1.ref_id_1 = q.ref_id_1

查询3:使用LEFT JOIN从user表和reference_2获取数据

select r2.ref_name,r2.ref_email,r2.ref_mobno,  
  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_nam e,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_2 r2
left join question q
on r2.ref_id_2 = q.ref_id_2

查询4:使用LEFT JOIN从user表和reference_3获取数据

select r3.ref_name,r3.ref_email,r3.ref_mobno,  
  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_3 r3
left join question q
on r3.ref_id_3 = q.ref_id_3

现在需要将所有4个查询合并为一个来显示数据。

我尝试在一个查询中加入所有这些查询

select u.user_name,u.user_emailid,u.user_mobno,

q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r1.ref_name,r1.ref_email,r1.ref_mobno,  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r2.ref_name,r2.ref_email,r2.ref_mobno,  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r3.ref_name,r3.ref_email,r3.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7

from user u
LEFT JOIN question q
on u.user_id = q.user_id
WHERE q.flag = 1
LEFT JOIN reference_1 r1
LEFT JOIN question q1
on r1.ref_id_1 = q1.ref_id_1
LEFT JOIN reference_2 r2
LEFT JOIN question q2
on r2.ref_id_2 = q2.ref_id_2
LEFT JOIN reference_3 r3
LEFT JOIN question q3
on r3.ref_id_3 = q3.ref_id_3

但是这个查询无效。

2 个答案:

答案 0 :(得分:1)

因为你搞砸了语法顺序......

多联接的工作原理如下:

select u.user_name,u.user_emailid,u.user_mobno,     q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.    que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r1.ref_name,r1.ref_email,r1.ref_mobno,   q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.    que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r2.ref_name,r2.ref_email,r2.ref_mobno,  q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.    que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,

r3.ref_name,r3.ref_email,r3.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.    que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7

from question q
LEFT JOIN user u USING(user_id)
LEFT JOIN reference_1 r1 USING(ref_id_1)
LEFT JOIN reference_2 r2 USING(ref_id_2)
LEFT JOIN reference_3 r3 USING(ref_id_3)
WHERE q.flag = 1

最后必须在哪里...... 联接必须完全陈述,不能混合。依赖顺序工作,你可以有子和子子等依赖,没有问题。

你做得非常好:在两个JOIN表中为外键设置相同的名称。 在这种情况下,您可以简单地替换r1.t1 = r2.t2 stuff并在函数USING(t1)中定义连接表。 :)

答案 1 :(得分:0)

尝试此查询。使用嵌套连接到引用表到问题表。

   SELECT u.user_name
    ,u.user_emailid
    ,u.user_mobno
    ,q.que1_name
    ,q.qa1
    ,q.que2_name
    ,q.qa2
    ,q.que3_name
    ,q.qa3
    ,q.que4_name
    ,q.qa4
    ,q.que5_name
    ,q.qa5
    ,q.que6a_name
    ,q.qa6a
    ,q.que6b_name
    ,q.qa6b
    ,q.que7_name
    ,q.qa7
    ,r1.ref_name
    ,r1.ref_email
    ,r1.ref_mobno
    ,r2.ref_name
    ,r2.ref_email
    ,r2.ref_mobno
    ,q.que1_name
    ,r3.ref_name
    ,r3.ref_email
    ,r3.ref_mobno
FROM dbo.user u
LEFT JOIN question q
LEFT JOIN reference_1 r1 ON r1.ref_id_1 = q.ref_id_1
LEFT JOIN reference_2 r2 ON r2.ref_id_2 = q.ref_id_2
LEFT JOIN reference_3 r3 ON r3.ref_id_3 = q.ref_id_3 ON u.user_id = q.user_id
WHERE q.flag = 1