加入表没有任何参考

时间:2015-01-15 00:00:39

标签: php mysql

在sql查询的结果中,我将检查php哪个列有记录。

示例:

  if ($column_1) {echo $column_1}
  if ($column_2) {echo $column_2}
  if ($column_3) {echo $column_3}

我不在那里使用else if,因为我需要它继续检查sql查询中的记录。

然而,使用上面的php代码,我无法联合表,以便从该表中获取另一个列记录。首先,因为列数不匹配。然后,即使我操纵列,它也会给我一个重复的行。

到目前为止,我一直在尝试实现此目的是使用子查询。但在我知道结果之前,它出现了错误。

嗯,这是我的完整代码。请看一下。

$sql = "SELECT      op.reference_no, op.eight_percent, op.ten_percent, 
                    op.date, op.claim, op.orders_history_id, 
                    oh.one_product_price, oh.quantity, 

                    (
                        SELECT      it.type 
                        FROM        infimoney_transfer it
                    ),
                    (
                        SELECT      it.receiveable 
                        FROM        infimoney_transfer it
                    ),
                    (
                        SELECT      oh.id AS ohid 
                        FROM        infimoney_transfer it
                    )

        FROM        order_promotion op

        LEFT JOIN   orders_history oh 
        ON          oh.id = op.orders_history_id

        WHERE       oh.customer_id = $member_id";


        $saldo = $mysqli->query($sql);

        if(!$saldo){ printf("Errormessage: %s\n", $mysqli->error); die(); }

        if ($saldo->num_rows > 0) {
            while($objek = $saldo->fetch_object()) {


                if ($objek->eight_percent) 
                {
                    $sourceCash = $objek->quantity % 10 > 4 ? 1 : 0;
                    echo "<tr>
                            <td>".$objek->reference_no . "</td>
                            <td>Cashback 8%</td>
                            <td>".$objek->date."</td>
                            <td>".$sourceCash * 5 * $objek->one_product_price."</td>
                            <td>".$objek->eight_percent."</td>
                            <td> - </td>
                            <td> Not Yet </td>
                         </tr>";
                }
                if ($objek->ten_percent) 
                {
                    $sourceCash = (int)($objek->quantity / 10);
                    echo "<tr>
                            <td>".$objek->reference_no . $objek->orders_history_id."</td>
                            <td>Cashback 10%</td>
                            <td>".$objek->date."</td>
                            <td>".$sourceCash * 10 * $objek->one_product_price."</td>
                            <td>".$objek->ten_percent."</td>
                            <td> - </td>
                            <td> Not </td>
                         </tr>";
                }
                if ($objek->claim) 
                {
                    $sourceCash = (int)($objek->quantity / 10);
                    echo "<tr>
                            <td>".$objek->reference_no . $objek->orders_history_id."</td>
                            <td>Claimed</td>
                            <td>".$objek->date."</td>
                            <td>".$objek->claim."</td>
                            <td> - </td>
                            <td>".$objek->claim."</td>
                            <td> Not </td>
                         </tr>";
                }

请帮助。提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您在同一个选择查询中加入所有表,则不会为您提供重复的行:

$sql = "SELECT  op.reference_no, op.eight_percent, op.ten_percent, 
                op.date, op.claim, op.orders_history_id, 
                oh.id, oh.one_product_price, oh.quantity, 
                it.type, it.receiveable, 
        FROM order_promotion op 
        LEFT JOIN orders_history oh ON oh.id = op.orders_history_id 
        LEFT JOIN infimoney_transfer it ON it.orders_list_id=oh.order_list_id 
        WHERE oh.customer_id = $member_id";