2011-06-21 90 views
1

這裏是我的數據庫結構:關於JOIN查詢在MySQL

payment option: 
    mshiptype_id 
    paymopt_Id 

membertopaymentoption: 
    member_Id 
    paymopt_Id 

mshiptypes 
    mshiptype_name 
    mshiptype_id 
    timetable_id 

timetables 
    timetable_id 
    timetable_Name 

timeslots 
    timeslot_id 
    timeslot_name 

timeslottotimetables 
    timeslot_id 
    timetable_id 

我想通過上面的表格

會不會有人給獲得單個成員

的mshiptype_name和timeslot_name和timetable_name建議加入查詢......在mysql中

+0

它是這樣的,但我想給定member_id – user682417

+0

我認爲你可以對這些表,瞧natutal加入mshiptype_name和timeslot_name和timetable_name! – Karolis

+0

我們怎麼想法? – user682417

回答

1
SELECT mshiptype_name, timeslot_name, timetable_name 
FROM mshiptypes ST 
INNER JOIN paymentoption PO ON ST.mshiptype_id = PO.mshiptype_id 
INNER JOIN membertopaymentoption MPO ON PO.paymopt_Id = MPO.paymopt_Id 
INNER JOIN timetables TT ON ST.timetable_id = TT.timetable_id 
INNER JOIN timeslottotimetables TTT ON TT.timetable_id = TTT.timetable_id 
INNER JOIN timeslotss TS ON TTT.timeslot_id = TS.timeslot_id 
WHERE MPO.member_id = <members_id> 
1

我想這是你想要的:

SELECT mshiptypes.mshiptype_name, timeslots.timeslot_name, timetables.timetable_Name 
FROM membertopaymentoption 
INNER JOIN paymentoption ON membertopaymentoption.member_Id=paymentoption.paymopt_Id 
INNER JOIN mshiptypes ON paymentoption.mshiptype_id=mshiptypes.mshiptype_id 
INNER JOIN timetables ON mshiptypes=timetable_id=timetables.timetable_id 
INNER JOIN timesslottotimetables ON timetables.timetable_id=timeslottotimetables.timeslot_id 
INNER JOIN timeslots ON timeslottotimetables.timeslot_id=timeslots.timeslot_id 
WHERE membertopaymentoption.member_Id=$id 

注:實際memeber ID

1

關鍵是識別常用於兩個不同的表的字段,並設法找到連接所有你想要的字段的路徑替換WHERE子句中$id

select 
    mtpo.member_Id, 
    mt.mshiptype_name, 
    ts.timeslot_name, 
    tt.timetable_name 
from 
    payment_option po 
    left join membertopaymentoption mtpo on po.paymopt_Id = mtpo.paymopt_Id 
    left join mshiptypes mt on mt.mshiptype_id = po.mshiptype_id 
    left join timetables tt on tt.timetable_id= mt.timetable_id 
    left join timeslottotimetables tstt on tstt.timetable_id = tt.timetable_id 
    left join timeslots ts on ts.timeslot_id = tstt.timeslot_id 
where 
    member_Id = 1 -- change this 

另外,我建議在太晚之前整理你的字段和表的名字。他們很混亂!

0
select 
    mshiptype_name, 
    timeslot_name, 
    timetable_name 
from 
    paymentoption 
    natural join membertopaymentoption 
    natural join mshiptypes 
    natural join timetables   
    natural join timeslottotimetables 
    natural join timeslots 
where 
    member_Id = 1 
+0

@ user682417這不行? – Karolis

1
select mt. mshiptype_name, ts. timeslot_name, tt. timetable_Name 
from membertopaymentoption mtp 
inner join payment p on mtp. paymopt_Id = p.paymopt_Id 
inner join mshiptypes mt on p.mshiptype_id = m.mshiptype_id 
inner join timetables tt on mt.timetable_id = tt. timetable_id 
inner join timeslottotimetables tstt on tt timetable_id = tstt. timetable_id 
inner join timeslots ts on tstt. timeslot_id = ts. timetable_id 
where mtp. member_Id = @memID //will be provided