2011-10-28 88 views
2

我有其中有兩個以人爲我想爲行返回PERSON_1,PERSON_2作爲新行,但與人的ID與百姓餐桌SQL連接兩個字段在一個表中

這是預訂表據我得到,但在預訂信息

SELECT people.* FROM (
    (select booking.person_1 as id from booking) 
    union ALL 
    (select booking.person_2 as id from booking) 
) as peopleids 
join people on people.id = peopleids.id; 

我的繼承人結構犯規拉

CREATE TABLE IF NOT EXISTS `booking` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `slot` enum('morning_drive','afternoon_loop','return_drive') NOT NULL, 
    `type` enum('911','vintage_911') NOT NULL, 
    `car` int(11) NOT NULL, 
    `person_1` int(11) DEFAULT NULL, 
    `person_2` int(11) DEFAULT NULL, 
    `dated` date NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; 


CREATE TABLE IF NOT EXISTS `people` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(50) NOT NULL, 
    `last_name` varchar(50) NOT NULL, 
    `organisation` varchar(100) NOT NULL, 
    `event_date` date NOT NULL, 
    `wave` varchar(20) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; 

如何,我能得到一個結果集喜歡 - person.first_name,person.last_name,人的任何想法.ORG anisation,booking.dated,person.car,person.slot。 IM具有兩個字段,並讓他們到它們與到一個列表

有興趣的人此更新,並在我的特定日期加入第3臺

繼承人我用PHP最終查詢瓦爾拉掙扎和插槽,並且還加入第三個表

SELECT peopleids.id, 
     peopleids.car, 
     cars.nr, 
     p.first_name, 
     p.last_name, 
     p.organisation, 
     p.event_date, 
     p.wave 
FROM (SELECT booking.car, booking.person_1 as id FROM booking WHERE booking.dated = '".$date."' AND booking.`slot` = '".$slot."' 
     union ALL SELECT booking.car, booking.person_2 as id FROM booking WHERE booking.dated = '".$date."' AND booking.`slot` = '".$slot."' 
    ) as peopleids 
LEFT JOIN people p ON p.id = peopleids.id LEFT JOIN cars on cars.id = peopleids.car; 
+1

參加人people.id = peopleids.id? – StuartLC

+0

這工作..但不允許我拉相關的預訂信息,最好的辦法是什麼? –

+0

你需要選擇其餘的工會領域(選擇booking.person1,booking.someotherfield,booking.bookingdate) – StuartLC

回答

1
SELECT 
     ag.id, 
     p.first_name, 
     p.last_name, 
     p.organisation, 
     p.event_date, 
     p.wave 
FROM (
     SELECT booking.person_1 as id, booking.Car as car FROM booking 
     union ALL 
     SELECT booking.person_2 as id, booking.Car as car FROM booking 
    ) as ag 
JOIN people p ON people.id = ag.id; 
INNER | LEFT JOIN Cars c ON c.ID = ag.car 
+0

我需要booking.car返回...和加入汽車表(我知道我從來沒有提到它在我的問題,但我認爲這將是很容易加入我自己的加入,但我堅持) –

+0

我不知道這兩個表是如何相關的,你想要返回什麼,我已經更新了我的查詢,也許它會給你一些指示如何實現你想要的 – sll

0
select people.first_name as firstname, 
      people.last_name as lastname, 
      people.organisation, 
      booking.dated, 
      booking.car, 
      booking.slot from booking 
left join people on booking.person_1 = people.id 

OR

select people.first_name as firstname, 
      people.last_name as lastname, 
      people.organisation, 
      booking.dated, 
      booking.car, 
      booking.slot 
from booking 
left join people on booking.person_1 = people.id or booking.person_2 = people.id 

檢查......如果你還需要幫助,可以幫助你