如题, 下面是我的试验结果
左表
select buildingNumCode,estateId from sell;
+-----------------+----------+
| buildingNumCode | estateId |
+-----------------+----------+
|            NULL |       12 |
|               0 |       11 |
+-----------------+----------+
右表
select buildingNumCode,estateId from estate_building;
+-----------------+----------+
| buildingNumCode | estateId |
+-----------------+----------+
|               0 |        1 |
|               1 |        1 |
|               0 |        2 |
|               0 |       11 |
|               0 |       12 |
|               0 |       10 |
|               0 |       11 |
+-----------------+----------+
连接后
select a.estateid,a.buildingNumCode from sell a         
        left join estate_building b on (a.buildingNumCode=b.buildingNumCode and a.estateId=b.estateId);
+----------+-----------------+
| estateid | buildingNumCode |
+----------+-----------------+
|       12 |            NULL |
|       11 |               0 |
|       11 |               0 |
+----------+-----------------+

解决方案 »

  1.   

    select buildingNumCode,estateId from estate_building;
    +-----------------+----------+
    | buildingNumCode | estateId |
    +-----------------+----------+
    |               0 |        1 |
    |               1 |        1 |
    |               0 |        2 |
    |               0 |       11 |
    |               0 |       12 |
    |               0 |       10 |
    |               0 |       11 |
    +-----------------+----------+
    这张表中本身就有两条。
      

  2.   

    >>
    这张表中本身就有两条。可是它是右表啊,难道我理解错了?
      

  3.   

    你理解错了。或者说你根本不理解JOIN运算。
      

  4.   

    先读三遍《数据库系统概论(第四版)》 王珊 萨师煊   高等教育出版社 (掌握基础知识和概念) 然后再粗略浏览一遍MYSQL的官方手册。(方便以后查找,避免类似于考试的时候,给你本政治书也不知道答案在第几章,第几页)MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
      

  5.   

    惭愧, 一直以为left join 返回记录数刚好等于左表记录数
      

  6.   

    如果我希望数量为左表数,像这种情况一般该用distinct吧?