简体   繁体   English

多个LEFT Join在mysql中有效但在php中不正确

[英]Multiple LEFT Join works correct in mysql but not in php

I tried mutiple LEFT JOIN in mysql it works fine there but however if use the same query in php mysqli object method I am not getting what I get in direct mysql 我在mysql中尝试了多个LEFT JOIN,但在那里工作正常,但是如果在php mysqli对象方法中使用相同的查询,我将无法获得直接mysql中的信息

HERE is the Query in 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 RESULT 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

Same query with php 与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);
}

I just var_dump the $row to see the result. 我只是var_dump $row以查看结果。 When I see I am not getting id for the 6th row as seen on mysql instead I get null 当我看到我没有在mysql上看到的第六行的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

I am expecting to get the id from the fruit table(which is 1 and the fruit name is mango) but its returning null . 我期望从水果表中获取ID (它是1,水果名称是mango),但是它返回null

Not sure why is that happening. 不知道为什么会这样。 Any help? 有什么帮助吗? fruit_stock TABLE fruit_stock表格

 id     f_id    stock_qty
 1      3       122
 2      2       322
 3      4       23
 4      5       12

orders Table orders

 id     fruit_id    qty
 1      2           3
 2      3           10
 3      4           15
 4      3           8

I don't want to care about id in other table I just want the main(fruits) table id and other corresponding whole data from other tables. 我不想关心其他表中的ID,我只想要main(fruits)表ID和其他表中的其他相应整个数据。

You can only have one key with a value of Id so when the query runs it is first 1 and then when it hits the 2nd Id it overwrites it with null note that you only have 1 Id in your array but two in the query. 您只能使用一个具有Id值的键,因此在查询运行时它是第一个1,然后当它遇到第二个Id ,它将以null覆盖它,请注意,数组中只有1个ID,而查询中只有两个。 You will need to be a bit more selective in the query and alias some of the columns. 您将需要在查询中更具选择性,并为某些列添加别名。

You can add alias to your ids or remove the useless ones (optional ones are commented): 您可以为ID添加别名或删除无用的ID(注释了可选的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';

Then you will have only one index id on your array. 这样,您的数组上将只有一个索引id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM