我建立了事件預訂網站的數據庫。現在我有一個關係到一到多用(eventTickets,eventSponsors,eventGuests)表關係活動表。如何獲得不同價值的每個表聯接
CREATE TABLE EVENT (
EVENT_ID INTEGER NOT NULL AUTO_INCREMENT,
ORGANIZER_ID INTEGER NOT NULL,
name VARCHAR(30) NOT NULL,
picture VARCHAR(50) NULL,
venue VARCHAR(50) NOT NULL,
city VARCHAR(30) NOT NULL DEFAULT Ethiopia,
sub_city VARCHAR(50) NULL,
location VARCHAR(50) NOT NULL,
category VARCHAR(50) NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
longitude DOUBLE NULL,
latitude DOUBLE NULL,
status VARCHAR(15) NOT NULL DEFAULT DRAFT,
discription TEXT NOT NULL,
country VARCHAR(30) NOT NULL DEFAULT Ethiopia,
end_time TIME NOT NULL,
start_time TIME NULL,
created_on TIMESTAMP NOT NULL DEFAULT TIMESTAMP(),
PRIMARY KEY (EVENT_ID),
FOREIGN KEY (ORGANIZER_ID) REFERENCES organizer(ORGANIZER_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB;
CREATE TABLE SPONSORS (
SPONSOR_ID INTEGER NOT NULL AUTO_INCREMENT,
EVENT_ID INTEGER NOT NULL,
name VARCHAR(50) NOT NULL,
image VARCHAR(50) NULL,
PRIMARY KEY (SPONSOR_ID),
FOREIGN KEY (EVENT_ID) REFERENCES event(EVENT_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB;
CREATE TABLE GUESTS (
GUEST_ID INTEGER NOT NULL AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
aka_name VARCHAR(50) NULL,
title VARCHAR(15) NULL,
bio VARCHAR(100) NULL,
image VARCHAR(50) NULL,
EVNT_ID INTEGER NOT NULL,
PRIMARY KEY (GUEST_ID),
FOREIGN KEY (EVENT_ID) REFERENCES event(EVENT_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB;
CREATE TABLE EVENT_TICKETS (
TICKET_ID INTEGER NOT NULL AUTO_INCREMENT,
EVENT_ID INTEGER NOT NULL,
name VARCHAR(20) NOT NULL,
type VARCHAR(20) NOT NULL,
price FLOAT(4) NOT NULL,
quantity INTEGER NOT NULL,
discription VARCHAR(255) NOT NULL,
sale_start DATE NOT NULL,
sale_end DATE NOT NULL,
active BOOL NOT NULL DEFAULT 0,
available INTEGER NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (TIK_ID)
FOREIGN KEY (EVENT_ID) REFERENCES event(EVENT_ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB;
現在我有事件表
EVENT_ID | NAME
1 | firstEvent
2 | secondEvent
發起人表
SPONSOR_ID | EVENT_ID | name
1 | 1 | firstEventSponsor
2 | 2 | secondEventSponsor
客桌
GUEST_ID | EVENT_ID | first_name | last_name
1 | 1 | firstEvent | guest
2 | 2 | secondEvent| guest
和票表
TICKET_ID | EVENT_ID | type
1 | 1 | free
2 | 2 | secondEvent
3 | 1 | paid
我想從其他每個表的與特定事件來獲取所有相關領域,如果記錄存在或NULL,如果是沒有的。當我做了加入我收到重複其他表的記錄,直到結果集錶行計數匹配
這裏獲得最大的回報是我連接的結果集
SELECT `event`.`EVENT_ID` AS 'eventId' ,`event`.`name` AS 'eventName' , `venue` `TICKET_ID` AS 'ticketId', `eventTicket`.`type` AS 'ticketType',
`eventGuest`.`GUEST_ID` AS 'guestId', CONCAT(`eventGuest`.`first_name`, " ", `eventGuest`.`last_name`) AS 'guestName',
`eventSponsor`.`SPONSOR_ID` AS 'sponsorId', `eventSponsor`.`name` AS 'sponsorName',
FROM `event`
LEFT OUTER JOIN `eventTicket` ON `event`.`EVENT_ID` = `eventTicket`.`EVENT_ID`
LEFT JOIN `eventGuest` ON `eventTicket`.`EVENT_ID` = `eventGuest`.`EVENT_ID`
LEFT JOIN `eventSponsor` ON `eventGuest`.`EVENT_ID` = `eventSponsor`.`EVENT_ID`
WHERE `event`.`EVENT_ID` = 1
GROUP BY `event`.`EVENT_ID`, `eventTicket`.`TICKET_ID`, `eventGuest`.`GUEST_ID`, `eventSponsor`.`SPONSOR_ID` ;
這個返回設置這樣的結果
eventId | eventName | ticketId | type | guestId | guestName | sponsorId | sponsorName
1 | firstEvent | 1 | free | 1 |firstEventGuest | 1 | firstEventSponsor
1 | firstEvent | 2 | paid | 1 |firstEventGuest | 1 | firstEventSponsor
這是重複事件的客人,活動的贊助紀錄行我想要得到的結果集是這樣的,如果可能
eventId | eventName | ticketId | type | guestId | guestName | sponsorId | sponsorName
1 | firstEvent | 1 | free | 1 |firstEventGuest | 1 | firstEventSponsor
1 | firstEvent | 2 | paid | NULL |NULL | NULL | NULL
我是我使用了錯誤的方法,或者它是默認的,請幫助我堅持這個挑戰/
第二排,你的票ID是2,但該類型的報酬? –
是啊,那正確的,但來賓ID和贊助商標識的代表,我認爲這可能是默認的行爲。如果它是任何解決方法? –
你的客桌應該有票的ID,以確定客戶有哪些門票。 –