--按 重庆,北京,上海,其他城市 的顺序排 select * from 表 order by charindex(a,'上海,北京,重庆') desc
select * from 表 order by charindex(a,'重庆,北京,上海') 大力猛。
假设你是要显示上海在前select * from 表 where a='上海' union select * from 表 where a<>'上海'
各位高手,select * from 表 order by charindex(a,'重庆,北京,上海'),不起作用啊!
order by case when a='重庆' then 1 when a='北京' then 2 ....
order by case when a='重庆' then 1 when a='北京' then 2 ....
to:yujohny(踏网无痕) 你方法不可以啦create table ai(id int,no nvarchar(100)) insert into ai select 105,'2' insert into ai select 105,'1' insert into ai select 103,'1' insert into ai select 105,'4' insert into ai select 105,'8'select * from ai where id=105 and no='4' union select * from ai where id=105 and no in('1','2') 你试试上面的数据就知道啦还是大力的方法好但是数据库里面有多个城市的话, 这句charindex(a,'重庆,北京,上海'......)就会很长,因为要把所有城市名字写上 麻烦!有没有,更简单的办法,人家只要一个城市开头,其他的循序无所谓
小黑的方法是正确的,select * from ft order by charindex(a,'上海,福建,南昌') desc 还是不能得到正确结果。
order by charindex(a,'重庆,北京,上海')
order by charindex(a,'重庆,北京,上海') 好像沒有排序功能吧.如果只是要某一記錄放在最前的話,倒可以用(比如重慶): order by charindex(a,'重慶')
如果你想上海在前面的话可以这样: select * from 表名 where a like '上海%' 这样就行了,它所返回的记录集全部是上海在前面的
--按照给定的字符串中的顺序来显示,包括在列表中的记录才显示 select charindex(com_name,'上海,北京,新疆,南京') ord,com_name from eb_company where charindex(com_name,'上海,北京,新疆,南京') > 0 order by ord -------- 1 上海 4 北京 7 新疆 10 南京
select * from 表 order by charindex(a,'上海,北京,重庆') desc
大力猛。
union
select * from 表 where a<>'上海'
case when a='重庆' then 1
when a='北京' then 2
....
case when a='重庆' then 1
when a='北京' then 2
....
你方法不可以啦create table ai(id int,no nvarchar(100))
insert into ai select 105,'2'
insert into ai select 105,'1'
insert into ai select 103,'1'
insert into ai select 105,'4'
insert into ai select 105,'8'select * from ai where id=105 and no='4'
union
select * from ai where id=105 and no in('1','2')
你试试上面的数据就知道啦还是大力的方法好但是数据库里面有多个城市的话,
这句charindex(a,'重庆,北京,上海'......)就会很长,因为要把所有城市名字写上
麻烦!有没有,更简单的办法,人家只要一个城市开头,其他的循序无所谓
还是不能得到正确结果。
好像沒有排序功能吧.如果只是要某一記錄放在最前的話,倒可以用(比如重慶): order by charindex(a,'重慶')
select * from 表名 where a like '上海%'
这样就行了,它所返回的记录集全部是上海在前面的
--按照给定的字符串中的顺序来显示,包括在列表中的记录才显示
select charindex(com_name,'上海,北京,新疆,南京') ord,com_name from eb_company
where charindex(com_name,'上海,北京,新疆,南京') > 0 order by ord
--------
1 上海
4 北京
7 新疆
10 南京