SSJGSS SSJGSS - 4 months ago 6
MySQL Question

Null values in My SQL query

My Query is

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


and my result is

enter image description here

in the Created column, i should not get the null values. are there any simple query to fix it.

Answer

Simply join all tables with 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'