Search code examples
phpmysqlmultiple-value

Select row where value is in column with multiple data separated comma


I have this scenario in my MySQL database: enter image description here

I need select row where tag is from url parameter (GET) with php-mysql query:

$tag = $_GET['tag'];
// example $tag = 1
// now I need select rows where in tags colums is value 1. 

How can I obtain this query? I think that I need create an array... but I dont know how do. thanks in advance!


Solution

  • Don't do it this way - it will create trouble for you with every query until you fix it.

    Instead, make a table of tags, and use a many-to-many relationship to associate teams with tags.

    For example

    CREATE TABLE tag (
        id int not null auto_increment primary key,
        name varchar(100),
        description varchar(255) );
    
    CREATE TABLE teamtag (
        team_id int,
        tag_id int,
        CONSTRAINT 'team_fk' FOREIGN KEY (team_id) REFERENCES team ('id'),
        CONSTRAINT 'tag_fk' FOREIGN KEY (tag_id) REFERENCES tag ('id') );