1

是否可以构建一组 SQL 命令来查询数据库?我所拥有的是三个表,每个表都有到期金额的列。想选择任何到期并显示在屏幕上的东西,以便可以开具发票(最好在表格中),并且每一行都有各自的客户会费。

我可以在三个表之间使用 UNION ALL 选择所有到期的内容,但是我不知道如何在表行中按 ID 列出它们。

以下是我到目前为止所拥有的。按照这个速度,我必须分别运行每个查询并将它们列在三个单独的列表中。建议?

      <table>
        <tr>
            <th> ID</th>
            <th> Cost 1</th>
            <th> Cost 2</th>
            <th> Cost 3</th>
        </tr>

      <?php 
    $list1 = "SELECT ID, Cost FROM Table1 WHERE Invoiced IS NULL;";
    //$list2 = "SELECT ID, Price2 FROM Table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate';";
    //$list3 = "SELECT ID, Price3 FROM Table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate'";

    $result = mysql_query($list1, $link) or die(mysql_error());

    $num_rows = mysql_num_rows($result);
    $num_fields = mysql_num_fields($result);

    for ($i=0; $i<$num_rows; $i++) {
        for ($j=0; $j<$num_fields; $j++) {
            $invoice[$i][mysql_fieldname($result,$j)] = mysql_result($result,$i,mysql_field_name($result,$j));
            }
        }

    //eventually the order it should be listed on screen
    for($i=0; $i<count($invoice); $i++) {
        echo "<tr><td>".$invoice[$i]["ID"]."</td>
        <td>".$invoice[$i]["Cost"]."</td>
        <td>".$invoice[$i]["Price2"]."</td>
        <td>".$invoice[$i]["Price3"]."</td></tr>";
    }

      ?>
      </table>

评论后编辑:

查询被传递并返回语法错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'all LEFT JOIN table1 ON all.ID = table1.ID LEFT JOIN t' at line 7

    $query = "
    SELECT all.ID, table1.Cost1, table2.Price2, tabl3.Price3
    FROM 
        (SELECT ID, Cost1 FROM table1 WHERE Invoiced IS NULL
            UNION
        SELECT ID, Price2 FROM table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate'
            UNION
        SELECT ID, Price3 FROM table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') AS all
    LEFT JOIN table1 ON all.ID = table1.ID
    LEFT JOIN table2 ON all.ID = table2.ID
    LEFT JOIN table3 ON all.ID = table3.ID
    ";
4

1 回答 1

2

从您在上面创建的将三个 Cost 列放在一行中的表格标题中ID,您似乎暗示您希望将JOIN三个表格放在一起ID。我在LEFT JOIN这里使用 a ,以确保所有行都Table1存在,即使其他两个表中的任何一个都没有相应的行。

SELECT
  Table1.ID,
  Table1.Cost as Cost1,
  Table2.Price2 AS Cost2,
  Table3.Price3 AS Cost3
FROM
  Table1 
  LEFT JOIN Table2 ON Table1.ID = Table2.ID
  LEFT JOIN Table3 ON Table1.ID = Table3.ID
WHERE 
  Table1.Invoiced IS NULL
  AND Table2.Expiration BETWEEN '$curDate' AND '$maxDate'
  AND Table3.Expiration BETWEEN '$curDate' AND '$maxDate'

评论后更新:

Table2可能有 aID未保存在Table1or的情况下Table3,例如(Table1.ID不能被认为是权威的),您可以DISTINCT ID通过 a 从所有 3 个表中获取总集UNION并使用它来加入:

SELECT
  allID.ID,
  Table1.Cost1,
  Table2.Price2 AS Cost2,
  Table2.Price3 AS Cost3
FROM
  /* Subquery gets a distinct set of IDs from all tables via UNION 
     so the outer query has a complete list to join against the other tables */
  (
    SELECT ID FROM Table1
    UNION SELECT ID FROM Table2
    UNION SELECT ID FROM Table3
  ) allID
  LEFT JOIN Table1 ON allID.ID = Table1.ID
  LEFT JOIN Table2 ON allID.ID = Table2.ID
  LEFT JOIN Table3 ON allID.ID = Table3.ID
/* Sorry, forgot the WHERE clause here */
WHERE
  Table1.Invoiced IS NULL
  AND Table2.Expiration BETWEEN '$curDate' AND '$maxDate'
  AND Table3.Expiration BETWEEN '$curDate' AND '$maxDate'

请注意,在一对一关系中存在具有几乎相同列结构的三个表可能意味着设计问题。您可以考虑将这些组合到一个表中。

关于 PHP 的进一步说明:

for在 PHP 中,我们几乎从未像在 C/C++ 中那样使用增量循环进行迭代。相反,我们通常使用foreachor 从查询中获取行时使用while循环。

// Fetch in a while loop
$invoice = array();
// $result is your query resource as you already have it...
while ($row = mysql_fetch_assoc($result)) {
  // Accumulate rows into $invoice array
  $invoice[] = $row;
}
// Then loop over the array:
foreach ($invoice as $inv) {
  echo "<tr>
    <td>{$inv['ID']}</td>
    <td>{$inv['Cost1']}</td>
    <td>{$inv['Cost2']}</td>
    <td>{$inv['Cost3']}</td>
  </tr>"; 
}

最终更新:

是的,该WHERE条款将限制满足的所有条件。如果你需要单独限制它们,你必须在子查询中这样做,然后将它们连接在一起,使用相同的UNION子查询来获得不同的集合ID

SELECT
  allID.ID,
  T1.Cost1,
  T2.Price2 AS Cost2,
  T3.Price3 AS Cost3
FROM
  (
    SELECT ID FROM Table1
    UNION SELECT ID FROM Table2
    UNION SELECT ID FROM Table3
  ) allID
  LEFT JOIN (SELECT ID, Cost AS Cost1 FROM Table1 WHERE Invoiced IS NULL) T1 ON allID.ID = T1.ID
  LEFT JOIN (SELECT ID, Price2 AS Cost2 FROM Table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') T2 ON allID.ID = T2.ID
  LEFT JOIN (SELECT ID, Price3 AS Cost3 FROM Table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') T3 ON allID.ID = T3.ID
于 2012-10-24T00:54:02.153 回答