0

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.

4
  • simply add created IS NOT NULL in where statement Commented Aug 2, 2016 at 10:30
  • wont work, throws ambiguity error Commented Aug 2, 2016 at 10:34
  • That's why you need to use invoice_payments.created in the subselect, not just created in main select... Commented Aug 2, 2016 at 10:36
  • @user1100671 The answer below worked for me . Thanks. Commented Aug 2, 2016 at 10:43

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.