【问题标题】:How to get result from multiple tables如何从多个表中获取结果
【发布时间】:2017-11-12 07:38:27
【问题描述】:

我在数据库中有三个表 incometax 、 servicetax 和 gst 以及三个表中的状态字段.. 我想知道三个表中有多少状态处于待处理状态..

<?php
    $sql ="SELECT i.status, s.status, g.status 
    FROM incometax i
    JOIN servicetax s 
    ON i.status = s.status
    JOIN gst g 
    ON s.status= g.status 
    WHERE status='Pending'";
    if ($result=mysqli_query($con,$sql))    
    {
       $rowcount=mysqli_num_rows($result);
       echo "$rowcount";
    }
?>

【问题讨论】:

  • 这个查询有什么问题
  • 提供的代码中存在连接,它怎么可能是那个问题的重复?作者在询问如何从每个表中的公共列名中获取值。

标签: php mysql mysqli phpmyadmin


【解决方案1】:

始终使用表别名作为列名的前缀。有三个表,因此有三个别名,因此有 3 个具有标题状态的唯一可识别列。要找到任何一个值为 pending 的,你需要一个这样的 where 子句:

WHERE i.status = 'pending' OR s.status = 'pending' OR g.status = 'pending'

请注意,如果您需要更多条件,则可能需要括号

WHERE (i.status = 'pending' OR s.status = 'pending' OR g.status = 'pending') AND ....

【讨论】:

    猜你喜欢
    • 2013-06-26
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多