如何将id与mysql中不同表的名称相匹配?

时间:2012-07-30 17:46:39

标签: php mysql

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匹配但不成功

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

任何帮助将不胜感激,非常新的php / mysql

3 个答案:

答案 0 :(得分:5)

尝试:

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

享受^ _ ^

答案 1 :(得分: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

答案 2 :(得分:0)

试试这个:

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