Mysql 如何使用逗号从数据库中选择数据
这是我需要的库存表。在这里,我想根据needgroupid选择Id和Name。这很好,但这里的问题是,如果我只传递了Mysql 如何使用逗号从数据库中选择数据,mysql,sql-server,sql-server-2008,Mysql,Sql Server,Sql Server 2008,这是我需要的库存表。在这里,我想根据needgroupid选择Id和Name。这很好,但这里的问题是,如果我只传递了12,15,那么它会给出输出。但是如果我只是传递12意味着我需要将输出作为两行,这两行分别是Markers和intel,但是失败了 如何基于逗号选择数据。MySQL:使用 示例1: SELECT FIND_IN_SET( 6, ',5,6,7,' ); -- results 3 SELECT FIND_IN_SET( '7', ',5,6,7,' ); -- results 4
12,15
,那么它会给出输出。但是如果我只是传递12
意味着我需要将输出作为两行,这两行分别是Markers
和intel
,但是失败了
如何基于逗号选择数据。MySQL:使用
示例1:
SELECT FIND_IN_SET( 6, ',5,6,7,' ); -- results 3
SELECT FIND_IN_SET( '7', ',5,6,7,' ); -- results 4
SELECT FIND_IN_SET( '8', ',5,6,7,' ); -- results 0
/** this should result `Markers` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,12,15,%';
/** this should result `Markers` and `intel` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,12,%';
/** this should result `Pen` and `cello` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,2,%';
MsSQL:使用 示例2:
SELECT FIND_IN_SET( 6, ',5,6,7,' ); -- results 3
SELECT FIND_IN_SET( '7', ',5,6,7,' ); -- results 4
SELECT FIND_IN_SET( '8', ',5,6,7,' ); -- results 0
/** this should result `Markers` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,12,15,%';
/** this should result `Markers` and `intel` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,12,%';
/** this should result `Pen` and `cello` **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,2,%';
请参阅:您可以使用如下查询:
SELECT * FROM `tableName` WHERE fieldName REGEXP '^9,|,9$|,9,' OR fieldName =9
有关更多信息,请参阅文档:尝试复制粘贴,为什么要在sqlfiddle中作为图像或帖子,并正确设置问题的格式@查查我的答案。