Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

database - MYSQL join comma separated query

I have searched around and came up with nothing.

I have 2 tables and to not have to query the database for every post that shows i need to join them somehow.

I want to get the url from the pics table that have the id of the pics field in posts table. Now heres my problem: the pics field is a commma separated "list" (4,1 or 32,4,32,2), because every post usually have more than one picture.

Table set ups:

posts:

 id | header | text | pics
| 1     xxx     xxx    3,1     
| 2     xxx     xxx    2,10,4     
| 3     xxx     xxx    16,17,18,19     
| 4     xxx     xxx    11,12,13        

pics:

id | name | url
| 1   xxx   xxx    
| 2   xxx   xxx        
| 3   xxx   xxx          
| 4   xxx   xxx          
| 10  xxx   xxx         
| 11  xxx   xxx         
| 12  xxx   xxx                  
| 13  xxx   xxx          
| 16  xxx   xxx          
| 17  xxx   xxx        
| 18  xxx   xxx        
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I strongly advise that you fix your current database structure so you are not storing the data in a comma separated list. You should structure your tables similar to the following:

CREATE TABLE posts
    (`id` int, `header` varchar(3), `text` varchar(3))
;

CREATE TABLE pics
    (`id` int, `name` varchar(3), `url` varchar(3))
;

CREATE TABLE post_pics
    (`post_id` int, `pic_id` int)
;

Then you can easily get a result by joining the tables:

select p.id,
  p.header,
  p.text,
  c.name,
  c.url
from posts p
inner join post_pics pp
  on p.id = pp.post_id
inner join pics c
  on pp.pic_id = c.id;

See SQL Fiddle with demo.

If you cannot alter your table, then you should be able to query using FIND_IN_SET:

select p.id, p.header, p.text, p.pics,
  c.id c_id, c.name, c.url
from posts p
inner join pics c
  on find_in_set(c.id, p.pics)

See SQL Fiddle with Demo.

Edit, if you want the data displayed as a comma-separated list then you can use GROUP_CONCAT.

Query 1:

select p.id,
  p.header,
  p.text,
  group_concat(c.name separator ', ') name,
  group_concat(c.url separator ', ') url
from posts p
inner join post_pics pp
  on p.id = pp.post_id
inner join pics c
  on pp.pic_id = c.id
group by p.id, p.header, p.text;

See SQL Fiddle with Demo

Query 2:

select p.id, p.header, p.text, p.pics,
  group_concat(c.name separator ', ') name,
  group_concat(c.url separator ', ') url
from posts p
inner join pics c
  on find_in_set(c.id, p.pics)
group by p.id, p.header, p.text, p.pics;

See SQL Fiddle with Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...