多个左连接在mysql中正确,但在php中不正确
我在mysql中尝试了多个左连接,但是如果在php mysqli object方法中使用相同的查询,我就无法得到直接mysql中得到的结果 以下是MYSQL中的查询多个左连接在mysql中正确,但在php中不正确,php,mysql,mysqli,Php,Mysql,Mysqli,我在mysql中尝试了多个左连接,但是如果在php mysqli object方法中使用相同的查询,我就无法得到直接mysql中得到的结果 以下是MYSQL中的查询 SELECT f.*,o.*,fs.* FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id MYSQL结果 id name price id fruit
SELECT f.*,o.*,fs.* FROM fruits f
LEFT JOIN orders o ON f.id = o.fruit_id
LEFT JOIN fruit_stock fs ON f.id = fs.f_id
MYSQL结果
id name price id fruit_id qty id f_id stock_qty
3 Banana 5 2 3 10 1 3 122
3 Banana 5 4 3 8 1 3 122
2 Apple 3 1 2 3 2 2 322
4 pomegranate 4 3 4 15 3 4 23
5 grape 3 NULLNULL NULL 4 5 12
1 mango 45 NULLNULL NULL NULLNULL NULL
与php相同的查询
$con1 = new mysqli('***','***','***','***');
$sel_sql = 'SELECT f.*,o.*,fs.* FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';
$result = $con1->query($sel_sql);
var_dump($result);
while($row = $result->fetch_assoc()){
var_dump($row);
}
我只是var\u dump
查看$row
的结果。当我看到mysql上的第6行没有得到id
,而是得到null
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '10' (length=2)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '8' (length=1)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '2' (length=1)
'name' => string 'Apple' (length=5)
'price' => string '3' (length=1)
'fruit_id' => string '2' (length=1)
'qty' => string '3' (length=1)
'f_id' => string '2' (length=1)
'stock_qty' => string '322' (length=3)
array (size=7)
'id' => string '3' (length=1)
'name' => string 'pomegranate' (length=11)
'price' => string '4' (length=1)
'fruit_id' => string '4' (length=1)
'qty' => string '15' (length=2)
'f_id' => string '4' (length=1)
'stock_qty' => string '23' (length=2)
array (size=7)
'id' => string '4' (length=1)
'name' => string 'grape' (length=5)
'price' => string '3' (length=1)
'fruit_id' => null
'qty' => null
'f_id' => string '5' (length=1)
'stock_qty' => string '12' (length=2)
array (size=7)
'id' => null
'name' => string 'mango' (length=4)
'price' => string '45' (length=2)
'fruit_id' => null
'qty' => null
'f_id' => null
'stock_qty' => null
我希望从水果表中获得id(它是1,水果名是mango),但是它返回null
不知道为什么会这样。有什么帮助吗?
水果库存
表格
id f_id stock_qty
1 3 122
2 2 322
3 4 23
4 5 12
订单
表格
id fruit_id qty
1 2 3
2 3 10
3 4 15
4 3 8
我不想关心其他表格中的id,我只想要主菜(水果)表id和其他表中其他相应的完整数据。您只能有一个值为
id
的键,因此当查询运行时,它首先是1,然后当它到达第二个id
时,它会用null
覆盖它。请注意,数组中只有1个id,但查询中有两个id。您需要在查询中更具选择性,并为某些列添加别名。您可以将别名添加到ID中或删除无用的别名(可选的会被注释):
然后,您的数组上只有一个索引
id
。您有多个id字段,它们相互覆盖。如果你想要所有这些,你必须给它们取别名。有3个id
列,它们是什么?最后一个数组中的id
似乎是第三个。使用alis来保持良好的状态id@olibiaz我把另一张桌子放在我的问题中information@rram看看我的答案please@nogad我已经用f,o,fs为表添加了别名。他们是否需要在alias中进行其他工作。我不能使用*从其他表中获取所有数据吗?谢谢,我猜,因为结果是一个关联数组,所以该值被覆盖了。我是否正确?谢谢您提供了有关每个id字段别名的附加信息,以避免覆盖。竖起大拇指
$sel_sql = 'SELECT
f.*,
-- o.id as order_id,
o.fruit_id,
o.qty,
-- fs.id as fruit_stock_id,
fs.f_id,
fs.stock_id
FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';