我有以下功能从表格发票中提取发票数据,但是如何从customers表格中获取与发票号码匹配的数据:
所以我想要得到* from customers tables where invoice = invoice
?
PHP
// the query
$query = "SELECT * FROM invoices ORDER BY invoice ASC";
// mysqli select query
$results = $mysqli->query($query);
// mysqli select query
if($results) {
print '<table class="table table-striped table-bordered" id="data-table" cellspacing="0"><thead><tr>
<th><h4>Invoice</h4></th>
<th><h4>Customer</h4></th>
<th><h4>Issue Date</h4></th>
<th><h4>Due Date</h4></th>
<th><h4>Status</h4></th>
<th><h4>Action</h4></th>
</tr></thead><tbody>';
while($row = $results->fetch_assoc()) {
print '
<tr>
<td>'.$row["invoice"].'</td>
<td>'.$row["customer_name"].'</td>
<td>'.$row["invoice_date"].'</td>
<td>'.$row["invoice_due_date"].'</td>
<td>test</td>
<td><a href="invoice-edit.php?id='.$row["invoice"].'" class="btn btn-primary">Edit</a> <a data-invoice-id="'.$row['invoice_id'].'" class="btn btn-danger delete-product">Delete</a></td>
</tr>
';
}
print '</tr></tbody></table>';
} else {
echo "<p>There are no invoices to display.</p>";
}
答案 0 :(得分:0)
试试这个..
$query = "SELECT c.customer_name, i.* FROM invoices as i JOIN customers as c ON i.customer_id = c.id ORDER BY i.invoice ASC";
这将根据您的需要为您提供客户名称。
答案 1 :(得分:0)
$query = "SELECT *
FROM Invoices I, Customers C
WHERE I.invoice = C.invoice
ORDER BY C.invoice ASC";
如果你更喜欢没有连接的声明..
答案 2 :(得分:0)
首先,通常最好将密钥包含在select
中(并且更容易排除故障),而不是*
。但是,假设这些是您的字段,您可以使用JOIN
连接这些字段,如下所示:
SELECT *
FROM invoices i
JOIN customer c
ON c.invoice = i.invoice
ORDER BY i.invoice
假设您在每个表上都有一个公共密钥(如您所建议的那样)是发票(这是使用数据库而不是平面文件的巨大优势),您可以将它们连接在一起({1} }和i
只是一个快捷方式,可以更容易地阅读查询。)
您也可以在c
行之后使用WHERE
对其进行修改,以缩小搜索结果范围,例如:(假设您要查找的编号为12345的特定发票
ON
SQL非常强大。如果您想了解更多信息,请访问此网站:http://www.w3schools.com/sql/
如果你想在你的查询中调用它(使用第一个例子)你就可以像你这样做,就像这样:
SELECT *
FROM invoices i
JOIN customer c
ON c.invoice = i.invoice
WHERE i.invoice = 12345
ORDER BY i.invoice
修改强>
将来最好在选择中声明您的密钥。假设 while($row = $results->fetch_assoc()) {
echo $row["invoice"];
}
在customer表中,您可以在select中将其称为customer_name
。但是,这是如何使用通配符从两个表中获取所有内容。将您的查询更改为:
c.customer_name