的数据合并到合并invoice-并从invoice
和一个查询clients
表对应的客户ADRESS数据我用这个语句:MySQL的:从3个表中一个结果有一个查询语句
SELECT *
FROM invoice, clients
WHERE invoice.client_id = clients.ID
它作品完美。但是现在我有第三个表格进入存储invoice_items的游戏。每张发票都有一个或多个客户将收取的物品。并且每个invoice_item UPDATE
都存储相应的以前生成的invoice.ID。但是如何在一个查询中合并3个表?我试过像这样:
SELECT *
FROM invoice, invoice_item, clients
WHERE inv_num =:num
AND invoice.client_id = clients.ID
AND invoice_item.inv_id = invoice.ID
但是到目前为止我没有成功。我做错了什么?
任何帮助表示赞赏。
编辑:整个语句如下所示:
$query = $this->db_con->prepare('SELECT * FROM invoice, invoice_item, clients WHERE inv_num =:num AND invoice.client_id = clients.ID AND invoice_item.inv_id = invoice.ID');
$query->bindValue(':num',$val, PDO::PARAM_STR);
$success = $query->execute();
$ val为从表之前选择的发票号码。
更新:
从Stivan的答案我得到以下结果模式。 假设有1张发票2 invoice_item的:
Array {
[0] => Array {
// col from table `invoice`
[col 1]
[col 2]
[col n]
// col from table `invoice_item`
[col 1]
[col 2]
[col n]
// col from table `clients`
[col 1]
[col 2]
[col n]
}
[1] => Array {
// col from table `invoice`
[col 1]
[col 2]
[col n]
// col from table `invoice_item`
[col 1]
[col 2]
[col n]
// col from table `clients`
[col 1]
[col 2]
[col n]
}
}
在每个阵列我从与表invoice
和clients
冗余内容的参与表all columns
内换句话说。
如何仅将clients
的列分隔为1 col
?
甚至更好保存的recources和invoice_items
从发票和附加的项目只收到1阵列*,所以它可能看起来像这样:
Array {
[0] => Array {
// col from table `invoice`
[col 1]
[col 2]
[col n]
// col from table `clients`
[col 1]
}
Array {
[0] => Array {
// col from table `invoice_items`
[col 1]
[col 2]
[col n]
}
[1] => Array {
// col from table `invoice_items`
[col 1]
[col 2]
[col n]
}
}
}
怎么不工作现有查询?它会产生错误还是只返回没有结果。发布您的表格结构,样本数据和预期结果可能会有所帮助。 – sgeddes
当我使用3个表的语句时,$ success是错误的 – Ben
为什么不使用连接? – Stivan