这是我需要的股票表。在这里,我想根据NeedGroupIds选择Id,Name。这很好,但问题是,如果我只传递12,15
,那么它会给出输出。但是,如果我只是通过12
,则意味着我需要将输出作为两行Markers
和intel
但是失败。
如何根据逗号选择数据。
答案 0 :(得分:0)
MySQL :使用FIND_IN_SET(str,strlist)
SELECT col1, col2, ... FROM my_table
WHERE FIND_IN_SET(value_to_find, NeedGroupIds);
/** if found, find_in_set should result in a number > zero **/
示例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
MsSQL :使用LIKE (Transact-SQL)
/** For non-mysql databases, an alternate to FIND_IN_SET **/
SELECT col1, col2, ... FROM my_table
WHERE ',' + NeedGroupIds + ',' LIKE '%,12,%';
示例2 :
/** 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,%';
答案 1 :(得分:0)
您可以使用如下查询:
SELECT * FROM `tableName` WHERE fieldName REGEXP '^9,|,9$|,9,' OR fieldName =9
有关详细信息,请参阅文档:http://dev.mysql.com/doc/refman/5.1/en/regexp.html