简体   繁体   English

选择值在列中的行,其中多个数据用逗号分隔

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

I have this scenario in my MySQL database: 我的MySQL数据库中有这种情况: 在此处输入图片说明

I need select row where tag is from url parameter (GET) with php-mysql query: 我需要使用php-mysql查询从URL参数(GET)选择标记所在的行:

$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! 提前致谢!

You can do that using LIKE / FIND_IN_SET but you definitely shouldn't. 您可以使用LIKE / FIND_IN_SET来做到这FIND_IN_SET但绝对不可以。

Instead consider changing schema and adding dictionary table to keep all tags and join table to keep all connections between tags and items. 而是考虑更改架构并添加字典表以保留所有标签,并添加联接表以保留标签和项目之间的所有连接。

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') );
SELECT * FROM `table` WHERE FIND_IN_SET(`Tags`, '$tag') > 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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