简体   繁体   中英

MySQL Count multiple values in same column

I have a script storing user generated tags ('keywords') in a database for each added video upload, however it places multiple keywords per item in the same keyword column, space separated. I'm not sure how this can be counted correctly. Clearly I'm no expert.

I have a table called ' video ', then within it

Vid      keyword  
------   -----------
1        cars 
2        Cars Audi 
3        Boats 
4        Trains  
5        Aircraft  

Is there a way to set it so the spaced value is counted separately?

If I do

SELECT keyword FROM video ORDER BY keyword ASC LIMIT 200

It returns

1. Aircraft 
2. Boats 
3. Cars Audi  
4. Trains

Is there a way to have it return;

1. Aircraft
2. Audi
3. Boats
4. Cars 
5. Trains 

And individually recognize the multiple spaced keywords in certain columns.

Thanks!

使用这种表结构很难做到这一点...您需要使用keywordvideo_id创建一个新表 。此表将具有一个primary key一个foreign key as video_id和一个keyword field ,该keyword field只能存储一个关键字

try that :

 select vid, substring_index(Keyword, ' ', -1)  as item1
 from video
 group by item1 
 order by item1

DEMO HERE

I do not think MySQL alone is capable of doing such a thing. You either need to add some script (I dont know if you are using a script or not) or change the structure of the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM