2013-05-30 124 views
1

有人可以幫我組合下面的2個查詢嗎?目標是在php中創建Speaker和Organizer變量(也在下面)。謝謝!結合2個查詢的MySQL

$query= " 
SELECT srMeet.mOrg, srMeet.mSpeaker, users.uID, users.uFirst, users.uLast 
FROM srMeet 
INNER JOIN users 
ON users.uID = srMeet.mSpeaker"; 

$query= " 
SELECT srMeet.mOrg, srMeet.mSpeaker, users.uID, users.uFirst, users.uLast 
FROM srMeet 
INNER JOIN users 
ON users.uID = srMeet.mOrg"; 


$Speaker = $row['uFirst'] . " " . $row['uLast']; 

$Organizer = $row['uFirst'] . " " . $row['uLast']; 

回答

1

假設一個滿足了一個揚聲器,你可以使用連接來獲得在一行中的相遇細節一個組織者。

$query= "SELECT srMeet.mOrg, 
      srMeet.mSpeaker, 
      UserSpeaker.uID AS SpeakerUid, 
      UserSpeaker.uFirst AS SpeakerFirst, 
      UserSpeaker.uLast AS SpeakerLast, 
      UserOrganizer.uID AS OrganizerUid, 
      UserOrganizer.uFirst AS OrganizerFirst, 
      UserOrganizer.uLast AS OrganizerLast 
     FROM srMeet 
     INNER JOIN users UserSpeaker 
     ON UserSpeaker.uID = srMeet.mSpeaker 
     INNER JOIN users UserOrganizer 
     ON UserOrganizer.uID = srMeet.mOrg"; 
+0

完美,謝謝! – user1041931

0

你爲什麼不使用UNION

$query= " 
SELECT srMeet.mOrg, srMeet.mSpeaker, users.uID, users.uFirst, users.uLast 
FROM srMeet 
INNER JOIN users 
ON users.uID = srMeet.mSpeaker 
UNION 
SELECT srMeet.mOrg, srMeet.mSpeaker, users.uID, users.uFirst, users.uLast 
FROM srMeet 
INNER JOIN users 
ON users.uID = srMeet.mOrg" 
+0

但是,這是如何幫助獲得我需要的兩個單獨的值?以前,我使用過類似「users.uFirst AS'的SpeakerFN'」,但我不知道如何在這裏做。 – user1041931

1

你可以參加在查詢同一個表多次使用AS給他們的別名。 AS關鍵字實際上是可選的,但我始終將其作爲對自己的說明。

這裏的INNER JOIN表示每個會議必須同時具有組織者和發言者。如果有機會在沒有組織者或演講者的情況下舉行會議,則可能需要使用LEFT OUTER JOIN s並執行一些操作來處理NULL值。

SELECT CONCAT(s.uFirst, ' ', s.uLast) AS speaker, 
    CONCAT(o.uFirst, ' ', o.uLast) AS organizer 
FROM srMeet 
INNER JOIN users AS s 
ON users.uID = srMeet.mSpeaker 
INNER JOIN users AS o 
ON users.uID = srMeet.mOrg; 


$Speaker = $row['speaker']; 

$Organizer = $row['organizer'];