2

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!

Vincenzo Lo Palo
  • 1,341
  • 5
  • 19
  • 32
  • 2
    The best option is for you to normalize your database; otherwise [FIND_IN_SET()](http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set) – Mark Baker Oct 15 '13 at 17:47
  • what do you mean for normalize my database? Can you do an example please? – Vincenzo Lo Palo Oct 15 '13 at 17:49
  • 1
    I mean use a team_tags table, comprising a team_id and a tag_id column, with one row for each tag associated with your team - as per D Mac's answer – Mark Baker Oct 15 '13 at 18:04

3 Answers3

4

You can do that using LIKE/FIND_IN_SET but you definitely shouldn't.

Instead consider changing schema and adding dictionary table to keep all tags and join table to keep all connections between tags and items.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
2

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') );
D Mac
  • 3,727
  • 1
  • 25
  • 32
0
SELECT * FROM `table` WHERE FIND_IN_SET(`Tags`, '$tag') > 0;
vaibhavmande
  • 1,115
  • 9
  • 17