加入3个表,第一个和第三个之间没有关系

时间:2012-08-06 13:32:56

标签: mysql

我想加入三张桌子

  1. patient_data - (p.name)
  2. employer_data - (e.name,e.city,e.stateAB)
  3. list_option_data - (l.state)
  4. patient_dataemployer_data有共同字段pid
    employer_datalist_option_data有comon字段stateAB

    所以我想显示p.name, e.name, e.city, l.state

    select
        p.name,
        e.name,
        e.city,
        l.state
    from patient_data as p
    inner join
     /*
        this part i need to know how to join these...
     */
    

1 个答案:

答案 0 :(得分:0)

你编码就像你解释的那样:

select
    p.name,
    e.name,
    e.city,
    l.state
from patient_data p
join employer_data e on e.pid = p.pid
join list_option_data l on l.stateAB = e.stateAB

请注意inner的{​​{1}}是多余的 - 默认联接类型为inner join