2012-07-30 90 views
0
A. event_choices 

event_id | res_id | 
4  | 10  | 

B. restaurants 

res_id  | res_name | 
10   | xyz  | 

C. event 
event_id | event_name | 
4   | birthday | 

我一直在試圖內連接嘗試匹配名字ID,但沒有成功如何將id與來自mysql中不同表的名稱進行匹配?

select event_id as id from event_choices inner join restaurants on res_id.id = res_name 

任何幫助,將不勝感激,很新的PHP/MySQL的

+0

你試圖得到什麼數據? – 2012-07-30 17:48:52

回答

2
SELECT event.event_name, restaurants.res_name FROM event_choices 
    INNER JOIN restaurants ON event_choices.res_id = restaurants.res_id 
    INNER JOIN event ON event_choices.event_id = event.event_id 

可以工作。

如果您選擇*而不是event.event_name您應該獲得所有數據並能夠從中選擇。您還可以選擇<table>.<field>形式的特定字段。

編輯:加入res_name

+0

可以很好地獲得結果中的event_name和res_name – Grundizer 2012-07-30 17:59:24

5

嘗試:

SELECT event_id, res_name FROM event_choices, restaurants WHERE event_choices.res_id = restaurants.res_id 

享受^ _^

+0

這很好,我怎麼修改這個以匹配event_id到e​​vent_name? – Grundizer 2012-07-30 17:51:26

+0

@Grundizer:只要加入'事件'在那裏。 'WHERE event.event_id = event_choices.event_id'。 – 2012-07-30 17:52:07

0

試試這個:

select ec.event_id as id from event_choices ec inner join restaurants r on ec.res_id = r.res_id 
相關問題