Web Hosting Geeks | Web Hosting Experts
Need help? Call us 1(866)TOP-HOST

14655 reviews 1735 hosting providers

Questions & Answers

selecting from tables and avoiding repetition

Dec 12, 2013 by GeekNew
Hello there,
I am using MySQL and phpmyAdmin and I have two tables that I want to select from without repetition  

Table 1 name
nid name
1 Sarah

Table 2 Subject
cid nid Subject
1 1 sub1
2 1 sub2
3 1 sub3

I select like this
 select * from name,subject where name.nid = subject.nid

and this is the result

nid name cid subject
1 Sarah 1 sub1
1 Sarah 2 sub2
1 Sarah  2 sub3

How do I avoid the repetition

so the result would look like

nid name subject
1 Adam sub1 , sub2 , sub3

can anyone please help?

2 Answers

0 votes
Dec 12, 2013 by lightSpeed
If you already know the name you can remove the name from the query and only have the subject for your results. This might help http://stackoverflow.com/questions/10021037/whats-the-best-way-to-combine-2-tables-in-mysql-and-remove-duplicates
0 votes
Dec 12, 2013 by tareq101
Try this
name.nid,name.name,group_concat(sub) as sub
name.nid = subject.nid group by car.nid