我的sql语句如下:select a.productname,a.sell_price,b.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034';把结果放在个$arr里,这时$arr[0]有值,$arr[1]有值,但$arr[2]里没值,用$arr['id']也拿不到。但是sql语句改到如下,$arr[2]里就有值了。select a.productname,a.sell_pricea.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034';也就是拿不到b表中的字段。这是什么原因?

解决方案 »

  1.   

     a.id=b.id  没有匹配的记录,不用奇怪的
      

  2.   

    select a.productname,a.sell_pricea.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034'; 
    这句明显是错误的a.sell_pricea.id
      

  3.   

    不可能,在数据库执行是有记录的而且如果没记录,那a.id也出不来
      

  4.   

    select a.productname,a.sell_price,a.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034'打字打错了
      

  5.   


    select b.productname,b.sell_price,a.id from umc_product_image a left join umc_product b on a.id=b.id where b.id='1034'; 
    改成这样试试
      

  6.   

    用的是left join ,umc_product_image 有没有纪录跟a.id是没有关系的
      

  7.   

    select * from umc_product_image where id='1034';看有没有记录?
      

  8.   

    select * from umc_product_image where id='1034';
    id 编号 productid 产品编号 color 图片颜色 imageurl 图片地址 pageshow 首页展示 ispass 审核通过 
    1034 479 0 ../../../uploads/479/125246559412345_479_min.JPG 0 1 select a.productname,a.sell_price,b.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034'; 
    productname  sell_price  id  
    护舒宝超值干爽丝薄夜用10片 6 1034 都是有记录的,到了php里b表的值就拿不到了
      

  9.   

    on  umc_product. 产品编号=umc_product_image . 产品编号umc_product.id 与umc_product_image.id估计是扯不上关系
      

  10.   

    确实,我sql语句写错了,应该这个
    select a.productname,a.sell_price,b.imageurl from umc_product a left join umc_product_image b on a.id=b.productid where a.id='$pid'但我不明白,虽然我碰巧取到值了,但为什么拿不出来?难道是哪里约束过了?
      

  11.   

    1、select a.productname,a.sell_price,b.id from umc_product a left join umc_product_image b on a.id=b.id where a.id='1034';
    2、select a.productname,a.sell_price,b.imageurl from umc_product a left join umc_product_image b on a.id=b.productid where a.id='$pid' 
    这两句没有区别,只是用于连接的键不同而已第一句并不是取不到 b.id 而是 umc_product_image 中没有 id='1034' 的记录
    第二句取到了 b.imageurl ,是因为 umc_product_image 中有 productid='$pid' 的记录left join 是左连接,其结果集中包括全部左表的记录和符合连接条件的右表记录,不符合连接条件时右表部分为空
    right join 右连接,正好反过来
    inner join 内连接,取两表的交集。即只取出满足连接条件的记录