My SQL查询中的空值

时间:2016-08-02 10:27:27

标签: mysql

我的查询

SELECT i.status,i.amount_total, i.id,sc.category as category,us.fname as name,us.company as company,
(SELECT transaction_id FROM invoice_payments WHERE invoice_payments.invoice_id = i.id) as transaction_id ,
(SELECT created  FROM invoice_payments WHERE invoice_payments.invoice_id = i.id and invoice_payments.created BETWEEN '2015-01-01' and '2016-01-01') as created 
FROM invoices i
inner JOIN 
users as us ON us.id = i.client_id 
inner JOIN subscriptioncategory as sc ON sc.user_id = us.id

我的结果是

enter image description here

在Created列中,我不应该得到空值。是否有任何简单的查询来解决它。

1 个答案:

答案 0 :(得分:1)

只需使用inner join加入所有表:

SELECT i.status,i.amount_total, i.id,sc.category as category,us.fname as name,us.company as company,
ip.transaction_id,
ip1.created 
FROM invoices i
inner JOIN 
users as us ON us.id = i.client_id 
inner JOIN subscriptioncategory as sc ON sc.user_id = us.id
join invoice_payments ip on ip.invoice_id = i.id 
join invoice_payments ip1 WHERE ip1.invoice_id = i.id and ip1.created BETWEEN '2015-01-01' and '2016-01-01'