【问题标题】:SQL SELECT CASE with Where From Looping ValueSQL SELECT CASE 与 Where From 循环值
【发布时间】:2021-03-31 03:15:58
【问题描述】:

我有 2 张桌子: 表名交易和详细客户

事务表字段为 idtrans 和 idcust。

详细客户字段是 idcust 和 custname。

我的 php 语法有问题,我想从值循环中选择带有条件的表,这是我的代码:

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
    }
    $sql="select * from transaction";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $sqlid="select * from detailcust where idcust='$row[idcust]')";
        $resultid= $conn->query($sqlid);
        if ($resultid->num_rows > 0){
        while ($rowid= mysqli_fetch_array($resultid)){
            $custname=$rowid["custname"];
    echo $custname;
    }

结果名称总是第一个 idcust 值。

【问题讨论】:

    标签: mysql while-loop conditional-statements case where-clause


    【解决方案1】:

    似乎想要至少有一笔交易的客户的姓名。如果是这样,则不需要循环和多个查询。只需一个查询即可获得所需的结果:

    select c.*
    from detailcust c
    where exists (select 1 from transaction t where t.idcust = c.idcust)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      相关资源
      最近更新 更多