有一张表是用户表,一张经销商表,还有一张城市表 
  用户表和经销商表有一个字段是city_id   是城市city_id的外键 
我现在要根据city_id   查出city_Name 
如果只是一张表不难,现在两张表都是一样的.我要怎么写才能分别求出城市名 
如果用户所在城市是北京   city_id   是1   经销商所在地是上海   city_id是2 
可是我一查的话就会两个全是北京,怎么解决这个问题userInfo  用户表
id vendorid username  cityid
1   1          张三   1vendorInfo 经销商表
vendorid   vendorname cityid
1       李四          2cityInfo 城市表
cityid   cityname
1         北京
2         上海得出
id,username,vendornaem,cityname_user,cityname_vendor
1   张三   李四   北京  上海

解决方案 »

  1.   

    LZ
    刚才的事情
    不好意思
    以后发帖
    就这样的..........
    o(∩_∩)o...
      

  2.   

    select a.username,b.vendorname ,(select cityname from cityInfo where cityid=a.cityid) as cityname_user,(select cityname from cityInfo where cityid=a.cityid) as cityname_vendor  from  userInfo as a,vendorInfo  as b where a.id=b.id 早写给你了不会变通的么?
      

  3.   

    select a.username,b.vendorname ,(select cityname from cityInfo where cityid=a.cityid) as cityname_user,(select cityname from cityInfo where cityid=a.cityid) as cityname_vendor  from  userInfo as a,vendorInfo  as b where a.id=b.id 
    测试下这个
    理论上是对的
      

  4.   

    SORRY有个错误select a.username,b.vendorname ,(select cityname from cityInfo where cityid=a.cityid) as cityname_user,(select cityname from cityInfo where cityid=b.cityid) as cityname_vendor  from  userInfo as a,vendorInfo  as b where a.id=b.id