richrock Posted November 28, 2008 Share Posted November 28, 2008 Hi, Been scratching my head over this for a while now. I have two tables, one purchases, the other won items. Both have invoice numbers attached to the items in each table, which may (or may not) be unique to each table... eg purchasesinvoicenumwon itemsinvoicenum Item 1INV-001Won Item 1INV-001 Item 2INV-002Won Item 2INV-003 And so on. In order to find all distinct Invoices in both tables, I need to join the tables, and find all DISTINCT() invoice numbers. I've never seen any LEFT JOIN query using more than one DISTINCT statement, so can it be done? Or am I going completely the wrong way? My thinking is (poor code follows): SELECT DISTINCT(inv_num) as invoices FROM purchases LEFT JOIN won_items ON inv_num GROUP BY purchases.invoices ORDER BY invoices DESC But this is wrong, so.... ??? TIA Quote Link to comment https://forums.phpfreaks.com/topic/134637-two-distinct-values-needed-in-same-query/ Share on other sites More sharing options...
fenway Posted November 28, 2008 Share Posted November 28, 2008 DISTINCT is *NOT* a function. Quote Link to comment https://forums.phpfreaks.com/topic/134637-two-distinct-values-needed-in-same-query/#findComment-701011 Share on other sites More sharing options...
Barand Posted November 29, 2008 Share Posted November 29, 2008 I'd use UNION here SELECT DISTINCT inv_num FROM `purchases` UNION SELECT DISTINCT inv_num FROM `won_items` ORDER BY inv_num Quote Link to comment https://forums.phpfreaks.com/topic/134637-two-distinct-values-needed-in-same-query/#findComment-701655 Share on other sites More sharing options...
richrock Posted December 2, 2008 Author Share Posted December 2, 2008 Sounds logical and good - I'll give it a shot and let you know! Quote Link to comment https://forums.phpfreaks.com/topic/134637-two-distinct-values-needed-in-same-query/#findComment-703831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.