我的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表中的字段。这是什么原因?
这句明显是错误的a.sell_pricea.id
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';
改成这样试试
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表的值就拿不到了
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'但我不明白,虽然我碰巧取到值了,但为什么拿不出来?难道是哪里约束过了?
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 内连接,取两表的交集。即只取出满足连接条件的记录