cyrus cyrus - 2 months ago 11
MySQL Question

MySQL select WITHOUT ignoring duplicates?

How would I select rows and display them WITHOUT ignoring any duplicate values? I just want them displayed as is regardless of the query or value repeating?

I have 2 DBs which I need to pull info from. The first DB has the Customer's Invoice number and the customer ID, from here I get the customer ID for all the invoices. Some customers might just have 1 Invoice while another customer might have 3 invoices. Now, once this data is pulled, I go to the second database and pull all the phone numbers of that customer using user id. Here is what my query looks like.

SELECT number FROM `db_name` WHERE `userid`=594 AND `customid`= 8 UNION
SELECT number FROM `db_name` WHERE `userid`=147 AND `customid`= 8 UNION
SELECT number FROM `db_name` WHERE `userid`=147 AND `customid`= 8 UNION
SELECT number FROM `db_name` WHERE `userid`=436 AND `customid`= 8


Now, as you can see, the middle 2 queries are repeating for the same userid, however I need them as is because this data goes into an excel sheet and there are 2 invoice numbers waiting for the customer telephone number as input in the column beside them.

The output of the query is:

9123456789
8987123456
9987654321


Is there a way I can get MySQL to give me the values regardless of them being duplicate queries? The only alternative I have is to use the output and then do a vlookup in excel for customer id, which is going to be a pain every time I want the results.

Thank you in advance for your help!

Answer
SELECT number FROM `db_name` WHERE `userid`=594 AND `customid`= 8 UNION ALL
SELECT number FROM `db_name` WHERE `userid`=147 AND `customid`= 8 UNION ALL
SELECT number FROM `db_name` WHERE `userid`=147 AND `customid`= 8 UNION ALL

SELECT number FROM db_name WHERE userid=436 AND customid= 8

Comments