是否可以构建一个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
";
答案 0 :(得分:2)
从您在上面创建的表格标题中,将{3}的三个“成本”列放在一行中,您似乎暗示您希望ID
将这三个表放在JOIN
ID
上LEFT JOIN
1}}。我在这里使用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
如果ID
可能Table1
没有Table3
或Table1.ID
,那么DISTINCT ID
不能被认为是权威的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'
从所有3个表中获取for
的总集合,并使用它来加入:
foreach
请注意,一对一关系中存在三个具有几乎相同列结构的表可能意味着设计问题。您可以考虑将这些组合到一个表中。
在PHP中,我们几乎从不使用增量while
循环进行迭代,就像在C / C ++中一样。相反,我们通常使用// 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
子查询将它们连接在一起,以获取{{1}}
{{1}}