-1

我正在编写如下查询:

select * 
from table 
where cityid=2

但问题是在我的数据库中,多个城市的 cityid 存储为 (1,2,3) 这个查询没有给我想要的结果。

4

3 回答 3

2

试试这个:如果val是你的参数

Select 
*
from table 
where cityId like 'val,%' 
or cityId like '%,val,%' 
or cityId like '%,val'
于 2013-10-24T13:43:51.293 回答
1

你可以使用 MySQL 的FIND_IN_SET()

SELECT * FROM table WHERE FIND_IN_SET(2, cityid) > 0;

但是,我实际上建议您更改表的架构,以便不在单个单元格中存储多个关联。您可以使用映射关系的第二个表来完成此操作(想想create table table_cities (table_id int, city_id int);),然后使用 ajoin来提取值:

SELECT
    t.*
FROM
    table t
    JOIN table_cities tc
        ON t.id = tc.table_id
WHERE
    tc.city_id = 2;
于 2013-10-24T13:45:58.450 回答
0

不确定我是否正确理解了您的要求。根据我的理解,试试这个:

select * from table where cityid like '%,2,%'; 
于 2013-10-24T13:44:31.920 回答