如何正确编写查询SELECT'ing多个表

时间:2017-07-06 20:15:31

标签: php mysql

请在下面查询。我想选择多个表。 entryId来自表stockcking。我打赌我应该使用JOIN或smt ..干杯!

$updateEntryId  = $_GET["entryId"];
$query          = "SELECT * 
                   FROM stockTracking, loggers, boxes 
                   WHERE entryId ='$updateEntryId'";
$result         = mysqli_query($connection, $query);

boxes|boxId boxQuantity boxName
        2           10      CL64

loggers | loggerId loggerQuantity loggerName
              2         10           34242342
stockTracking| entryId  time        destination reference
              2        timestamp        Paris       1312

我已将查询更新到以下内容,但是我的WHERE语句有什么问题? 当我添加WHERE entryId ='$ updateEntryId'如果无法显示任何结果

$updateEntryId  = $_GET["entryId"];
$query          = "SELECT * 
               FROM stockTracking
               JOIN loggers
               ON entryId=loggerId 
               JOIN boxes
               ON boxId=entryId 
               WHERE entryId='$updateEntryId'";

3 个答案:

答案 0 :(得分:0)

是的,你是对的。您应该在下面的表中执行JOIN操作。将以下示例sql代码中的some_column替换为您在表之间存在关系的实际列名

SELECT s.* 
FROM stockTracking s
JOIN loggers l ON s.some_column = l.some_column
JOIN boxes b ON b.some_column = s.some_column
WHERE s.entryId ='$updateEntryId'

答案 1 :(得分:0)

试试这个:

$updateEntryId  = $_GET["entryId"];
$query          = "SELECT * 
               FROM stockTracking s
               JOIN loggers l
               ON s.entryId=l.loggerId 
               JOIN boxes b
               ON b.boxId=s.entryId 
               WHERE s.entryId='".$updateEntryId."'";

加入@rahul所说的表格,但应该以适当的方式进行..欢呼!

答案 2 :(得分:0)

解决。 问题发生在$updateEntryId=$_GET["entryId"]; 它没有结果,因为搜索栏显示http://stock/edit.php?id=2时,它正在寻找$_GET["id"]而不是$_GET["entryId"]