How to combine 2 mysql query ?

Hi,

May i know how to combine these 2 mysql query in one go ?

SELECT * FROM tblhosting WHERE nextduedate > nextinvoicedate;

Open in new window


SELECT * FROM tblinvoiceitems INNER JOIN tblinvoices ON tblinvoiceitems.invoiceid = tblinvoices.id WHERE relid =  'x' AND STATUS =  'unpaid';

Open in new window


relid =  'x'  , x is the "id" in "SELECT * FROM tblhosting"

Both are in the same database but 2 separate table.

If using mysql query is not possible, please help me to construct in PHP query.

Thank you.
LVL 2
Shamsul KamalJunior TechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
Try something like:
SELECT * FROM tblhosting, tblinvoices, tblinvoiceitems WHERE
   tblhosting.nextduedate > tblhostingnextinvoicedate AND
   tblinvoices.id = tblinvoiceitems.invoiceid AND
   relid = tblhosting.id AND
   STATUS = 'unpaid';

Open in new window


You might need to preface the 'relid' and 'STATUS' fields with the table name, if they are ambiguous.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shamsul KamalJunior TechAuthor Commented:
Hi,

I have tried the following :

SELECT * FROM tblhosting, tblinvoices, tblinvoiceitems WHERE
   tblhosting.nextduedate > tblhosting.nextinvoicedate AND
   tblinvoices.id = tblinvoiceitems.invoiceid AND
   tblinvoiceitems.relid = tblhosting.id AND
   tblinvoices.status = 'unpaid';

Open in new window


But the output seems wrong.

Can anybody spot the problem and help me ?

Thank you.
0
NerdsOfTechTechnology ScientistCommented:
This is guesswork unless we have some sample input data and expected output.

 SELECT * 
 FROM tblinvoiceitems ii
 INNER JOIN tblinvoices i
 ON ii.invoiceid = i.id
 LEFT JOIN tblhosting h
 ON ii.relid = h.id
 WHERE 
 h.nextduedate > h.nextinvoicedate AND 
 i.relid =  'x' AND 
 i.STATUS =  'unpaid';

Open in new window

0
Jan LouwerensSoftware EngineerCommented:
What resolution are your dates stored at? Perhaps the issue is this statement:
tblhosting.nextduedate > tblhosting.nextinvoicedate

Open in new window


Maybe what you need is something like:
tblhosting.nextduedate >= tblhosting.nextinvoicedate

Open in new window

0
Shamsul KamalJunior TechAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.