1

有人可以帮我结合下面的两个查询吗?目标是在 php 中创建 Speaker 和 Organizer 变量(也在下面)。谢谢!

$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'];
4

3 回答 3

1

您可以在查询中多次加入同一个表,并使用AS. 该AS关键字实际上是可选的,但我总是将其作为自己的注释。

这里的INNER JOINs 表示每次会议必须同时有组织者和发言人。如果有可能在没有组织者或演讲者的情况下召开会议,您可能希望使用LEFT OUTER JOINs 并做一些事情来处理 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'];
于 2013-05-30T15:49:12.257 回答
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";
于 2013-05-30T15:52:28.833 回答
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"
于 2013-05-30T15:42:06.683 回答