Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql/php - Tag System

Tags:

php

mysql

I'm implementing a Tag system...

Currently i have the table tags with name, description, createdby fields.

And the table images that have a field tags. Inside this field i will put the tags name from the table tags, separated by comma.

But, if i want to get all images with tag Foo how i do form the query? since the field will contain multiple tags separated by comma.

like image 414
Fr0z3n Avatar asked Dec 14 '25 20:12

Fr0z3n


2 Answers

Not sure if you have thought about normalizing the images table, but that would be the optimal solution here.

Instead of having

enter image description here

Structure your table this way

enter image description here

Then you can run this SQL statement to return all images with a certain tag

SELECT * FROM images
WHERE tags = 'forest';

Or if there is a reason you need to keep your table in the current format, you can use

SELECT * FROM images
WHERE tags LIKE '%forest%';

The word 'forest' in the above 2 examples is the tag you are searching for

like image 200
Lloyd Banks Avatar answered Dec 16 '25 11:12

Lloyd Banks


Try something like that :

SELECT * FROM images WHERE tags = 'Foo' OR tags LIKE 'Foo,%' OR tags LIKE '%,Foo' OR tags LIKE '%,Foo,%'
like image 44
berty Avatar answered Dec 16 '25 11:12

berty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!