city_name是字段名,现在要按("北京","上海","广州") 先后顺序把数据查询统计出来?
不知道你什么数据库, 假如是 Oracle:
select
count(city_name),
city_name
from
tuan_info
where
source_type = 1
and city_name in("北京","上海","广州")
group by
city_name
ORDER BY
INSTR('北京,上海,广州', city_name);
如果是 SQL Server 用
ORDER BY CHARINDEX(city_name, '北京,上海,广州')
追问
是SQL Server 的,怎么写哦,给个完整点的语句好吗
回答
select
count(city_name),
city_name
from
tuan_info
where
source_type = 1
and city_name in("北京","上海","广州")
group by
city_name
ORDER BY CHARINDEX(city_name, '北京,上海,广州')
前面的SQL都是一样的啊。
只是 增加一个 ORDER BY 的排序处理。
追问
在吗,不行的,提示有错啊,是什么问题哦
[SQL] select count(city_name), city_name from tuan_info where source_type = 1 and city_name in("北京","上海","广州") group by city_name ORDER BY CHARINDEX(city_name, '北京,上海,广州');
[Err] 1305 - FUNCTION data_group.CHARINDEX does not exist
回答
你这个是什么数据库啊?
CHARINDEX 是 SQL Server 自带的函数啊。
追问
那个,,是MYSQL的哦,怎办
回答
MySQL 就换一个函数 INSTR
select
count(city_name),
city_name
from
tuan_info
where
source_type = 1
and city_name in("北京","上海","广州")
group by
city_name
ORDER BY INSTR( '北京,上海,广州', city_name)