Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Simple Join and I'm stumped

Tags:

sql

join

I have a table with columns:

JOB_NUM, HM_PH, BUS_PH, CALL1ST

Where the job_num is a unique number

And the HM_PH, BUS_PH, and CALL1ST columns are 10 digit phone numbers

So using the order of columns above, sample data would look like:

JOB_NUM, HM_PH,      BUS_PH,     CALL1ST
------------------------------------
12345,   4025557848, 9165897588, 7518884455  
10101,   8887776655, 8667416895, 5558884446

What I want to produce is 2 columns.

JOB_NUM, PHONE

Where the job_num is listed next to every phone number such as:

JOB_NUM PHONE
---------------------
12345   4025557848  
12345   9165897588  
12345   7518884455  
10101   8887776655  
10101   8667416895  
10101   5558884446  

Where do I start?

like image 810
CodingIsAwesome Avatar asked Dec 17 '22 22:12

CodingIsAwesome


1 Answers

You need a UNION (if you want to remove duplicate rows) or UNION ALL (if you want to keep duplicate rows):

SELECT JOB_NUM, HM_PH AS PHONE FROM yourtable
UNION
SELECT JOB_NUM, BUS_PH FROM yourtable
UNION
SELECT JOB_NUM, CALL1ST FROM yourtable
ORDER BY JOB_NUM
like image 88
Mark Byers Avatar answered Dec 28 '22 06:12

Mark Byers