2011-10-28 62 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 

检查......如果你还需要帮助,可以帮助你