0

I have two tables, one is called contacts and the other one is called numbers. One stores contact information and looks like this

contacts
-------------------------------------------------------
| id | fname | lname | email | address | uid | uniqid |
-------------------------------------------------------

My second table which stores phone numbers that belong to specific contact look like this

numbers
---------------------
| id | number | cid |
---------------------

The cid is the same as the uniqid on contact table, how can i get the contact row with its numbers which is on the second table through mysql?

Update Correction to the correct answer

SELECT id ,fname ,lname ,email ,address , uid, uniqid,number
FROM contacts a
inner join (SELECT cid, GROUP_CONCAT(DISTINCT number SEPARATOR ',') number FROM numbers) b ON b.cid=a.uniqid

It was missing DISTINCT

1
  • Show us sample data for both tables, and also the expected result. All as formatted text, not images! Commented Apr 2, 2019 at 8:15

6 Answers 6

2

use join

select id ,fname ,lname ,email ,address , uid, uniqid,number
from contacts a
inner join numbers b on b.cid=a.uniqid
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for help, but unfortunately it creates another row if theres another number with the same uniqid
1

You can use GROUP_CONCAT to get multiple numbers to one row and then when you imply the join you won't get duplicates.

select `id` ,`fname` ,`lname` ,`email` ,`address` , `uid`, `uniqid`,`number`
from `contacts` a
inner join (Select `cid`, GROUP_CONCAT(`number` seperator ',') `number` from `numbers`) b on b.cid=a.uniqid

2 Comments

Not working for some reason, i tried to capitalize the syntax. but nothing
could you be able to try now, I think it's because number is a keyword so enclose it and execute.
1

You can map the two id's make sure you have this as table index, for faster retrieval of data.

SELECT id ,fname ,lname ,email ,address , uid, uniqid, number from contacts a, number b WHERE a.uniqid = b.cid;

1 Comment

Tip of today: Switch to modern, explicit JOIN syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.
1

Just use inner join with n.cid = c.uniqid

select c.id,c.fname,c.lname,c.email,c.address,c.uid,c.uniqid,n.number 
from contacts c 
inner join numbers n 
on n.cid = c.uniqid

Comments

1

using join is the right choice here:

  SELECT con.*,num.* from contacts as con inner join numbers as num on con.uniqid = num.cid

Comments

1

Here we are using the concept of foreign key . Here cid is foreign key of contact table on number table. we have to match primary key of contact table with the foreign key of number table. if both are match then it's show the result.

Select a.id, a.fname, a.lname, a.email, a.address, 
a.uid, a.uniqid,b.number from contact a, number b where a.id=b.id;

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.