Php 如何按名称列出两个已联接的表信息(有些带有名称,有些可能为空)
我有两张这样的桌子Php 如何按名称列出两个已联接的表信息(有些带有名称,有些可能为空),php,mysql,sql,select,Php,Mysql,Sql,Select,我有两张这样的桌子 Invoice (invoice_no, invoice_date, customer_id, ...), Customers (customer_id, customer_name, ...) 现在我要做的是列出按客户名称订购的发票 SELECT b.customer_name, a.* FROM Invoice a, Customers b WHERE a.customer_id=b.customer_id ORDER BY b.customer_name 但
Invoice (invoice_no, invoice_date, customer_id, ...),
Customers (customer_id, customer_name, ...)
现在我要做的是列出按客户名称订购的发票
SELECT b.customer_name, a.*
FROM Invoice a, Customers b
WHERE a.customer_id=b.customer_id
ORDER BY b.customer_name
但是这个sql的问题是,如果有没有客户id的发票
,
如何首先列出这些发票,并通过
customer\u name
asc列出具有customer\u id
的发票。使用LEFT JOIN
“有点”奇怪。为什么会有一些没有客户的发票?你要把它发给谁?不管怎样,问题是这样的
SELECT a.*, b.* // SELECT only the columns you want
FROM Invoice a
LEFT JOIN Customers b
ON a.customer_ID = b.customer_ID
要全面了解加入,请访问以下链接:
SELECT * FROM Invoice,customer where Invoice.customer_id=customer.customer_id ORDER BY IF(Invoice.customer_id is NULL , Invoice.customer_id, ~Invoice.customer_id) ASC
这是什么<代码>~Invoice.customer\u id我试图运行该语句,但它会生成未知的随机数。无论如何,OP对数据的排序不感兴趣。
SELECT * FROM Invoice,customer where Invoice.customer_id=customer.customer_id ORDER BY IF(Invoice.customer_id is NULL , Invoice.customer_id, ~Invoice.customer_id) ASC