2011-10-28 4 views
2

j'ai table réservations qui a deux personnes- je veux revenir PERSON_1 comme une ligne, PERSON_2 comme une nouvelle ligne mais avec l'identifiant de la personne liée à la table des personnessql rejoindre sur deux champs dans une table

C'est pour autant que je suis arrivé, mais ne marche pas tirer dans les informations réservation

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; 

Heres ma structure

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 ; 

des idées sur la façon dont je pourrais obtenir un jeu de résultats person.first_name comme-, person.last_name, personne .org anisation, booking.dated, person.car, person.slot. im aux prises avec avoir deux champs et les avoir à les relier dans une liste

mise à jour pour toute personne intéressée par cela et se joindre à une 3ème table de

Heres ma requête finale avec php vars pour tirer dans mes certaines dates et machines à sous et adhèrent également à une troisième table

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

rejoindre les gens sur people.id = peopleids.id? – StuartLC

+0

cela fonctionne .. mais ne me permet pas de tirer les informations de réservation connexes, quelle est la meilleure façon? –

+0

vous devez sélectionner le reste des champs dans vos deux syndicats (sélectionnez booking.person1, booking.someotherfield, booking.bookingdate) – StuartLC

Répondre

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

J'ai besoin de booking.car à retourner ... et joindre à la table des voitures (je sais que je ne l'ai jamais mentionné dans ma question mais je pensais qu'il serait facile d'ajouter ma propre participation, mais je suis coincé) –

+0

Je ne sais pas comment les deux tables sont liées et ce que vous voulez exactement revenir, J'ai mis à jour ma requête peut-être que cela vous donne quelques conseils pour réaliser ce que vous voulez – 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 

OU

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 

vérifier que ... si vous avez encore besoin d'aide vous aidera à

Questions connexes