如何组合2个sql语句

时间:2014-07-17 05:51:13

标签: sql

我正在尝试从他们拥有的旧数据库中检索客户端的数据。我需要的数据是这两个sql语句的混合。我如何将它们组合起来从第二个语句中的enrol_stud表和Stud_Haddress表中获取所有数据。

声明1 - 我希望Enrol_Stud和Stud_Haddress中的所有数据都包含在声明2中

select enrol_stud.*, stud_haddress.*
from enrol_stud_forms 
inner join enrol_stud on enrol_stud_forms.student_id = enrol_stud.student_id

声明2我希望以下声明中包含上述2个表中的数据。

SELECT contact.*, vsmc.*, concarer1.*, salcarer2.*,  contact.firstname ||' 
'||contact.surname as STUDENT, salcarer1.salutation as CARER1_TITLE, concarer1.firstname as CARER1_FIRSTNAME, concarer1.surname  as CARER1_SURNAME, concarer1.email_address as CARER1_EMAIL, salcarer2.salutation as CARER2_TITLE, concarer2.firstname as CARER2_FIRSTNAME, concarer2.surname as CARER2_SURNAME, concarer2.email_address as CARER2_EMAIL

FROM get_currently_enroled_students ('now') gces
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1 on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2 on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname

我知道它在连接中,我无法弄清楚如何包含它们。

1 个答案:

答案 0 :(得分:0)

你可以尝试如下,包括每个帖子的第一个语句中的列,以及如下所示的连接

SELECT contact.*, 
vsmc.*, 
enrol_stud.*, <- here
stud_haddress.*, <-here
concarer1.*, 
salcarer2.*,  
contact.firstname ||' '||contact.surname as STUDENT, 
salcarer1.salutation as CARER1_TITLE, 
concarer1.firstname as CARER1_FIRSTNAME, 
concarer1.surname  as CARER1_SURNAME, 
concarer1.email_address as CARER1_EMAIL, 
salcarer2.salutation as CARER2_TITLE, 
concarer2.firstname as CARER2_FIRSTNAME, 
concarer2.surname as CARER2_SURNAME, 
concarer2.email_address as CARER2_EMAIL

FROM get_currently_enroled_students ('now') gces
INNER JOIN enrol_stud_forms on gces.student_id = enrol_stud_forms.student_id <- here
INNER JOIN enrol_stud on enrol_stud.student_id = gces.student_id <- here
INNER JOIN student on gces.student_id = student.student_id
INNER JOIN contact on student.contact_id=contact.contact_id
INNER JOIN view_Student_mail_carers vsmc on student.student_id=vsmc.student_id
INNER JOIN contact concarer1 on vsmc.carer1_contact_id=concarer1.contact_id
INNER JOIN contact concarer2 on vsmc.carer2_contact_id=concarer2.contact_id
INNER JOIN salutation salcarer1 
on concarer1.salutation_id=salcarer1.salutation_id
INNER JOIN salutation salcarer2 
on concarer2.salutation_id=salcarer2.salutation_id
ORDER BY contact.surname, contact.firstname