DISTINCT的问题,请帮忙!
我想选择出产品表中star值为1,产品名称不重复的记录,且排列顺序要从新到旧
我是这样写的,可是提示总是出错,请各位兄弟帮帮忙,我再此谢了!
sql="select DISTINCT name from products where star='1' order by sn DESC"
rstar.Open sql,conn,1,1
这语句该怎么写呀
问题点数:20、回复次数:17Top
1 楼wuxfbs()回复于 2002-03-28 14:33:11 得分 0
sql="select DISTINCT * from products where star='1' order by sn DESC"
Top
2 楼Bullforg(...)回复于 2002-03-28 14:39:01 得分 0
这样写不行呀,我试过的,它说DISTINCT只能指定列名,也就是给定字段名
Top
3 楼x_zing(阿浩)回复于 2002-03-28 14:49:06 得分 5
看下面的提示:
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
你自己的语句出问题是因为sn不在你选择的列中,
wuxfbs() 的语句应该是没问题的,除非表中没有star或sn的字段或者star不是字符型的,不过可能不符合你的要求。
把你的表结构写出来,可以给你一个答案。
Top
4 楼Bullforg(...)回复于 2002-03-28 14:56:06 得分 0
不是呀,SELECT执行后,在选择出的内容中还是有名称重复的呀Top
5 楼8992026(8992026)回复于 2002-03-28 15:00:05 得分 0
select distinct name from (
select * from products where star='1' order by sn DESC
)
Top
6 楼x_zing(阿浩)回复于 2002-03-28 15:01:53 得分 0
select distinct name,sn from products where star='1' order by sn DESC
行不行?
不行的话这个:
select name from (select distinct name from products where star='1') t left join products on t.name=products.name order by products.sn descTop
7 楼sky_blue(蓝天2007)回复于 2002-03-28 15:03:27 得分 5
select DISTINCT rtrim(ltrim(name)) from products where star='1' order by sn DESCTop
8 楼8992026(8992026)回复于 2002-03-28 15:04:36 得分 0
以上错误!Top
9 楼Bullforg(...)回复于 2002-03-28 15:05:44 得分 0
id 为自动编号
name
star
ptype
logo
brief
apply
usage
dosage
label
amount_type
amount
exo_package
package
exo_box1
exo_box2
sn (int)Top
10 楼Bullforg(...)回复于 2002-03-28 15:07:34 得分 0
因为我现在是在修改以前同事做的东西,嗨,实在是够乱!
数据库和程序都是以前的!我连说明都没有!Top
11 楼x_zing(阿浩)回复于 2002-03-28 15:07:40 得分 0
或者:
select distinct name from (select * from products where star='1' order by sn desc) tTop
12 楼8992026(8992026)回复于 2002-03-28 15:13:13 得分 5
x_zing(阿浩) 我刚测试过,select distinct name from (select * from products where star='1' order by sn desc) t有错误。
子查询只有同时指定了 TOP,才可以指定 ORDER BY。
Top
13 楼Bullforg(...)回复于 2002-03-28 15:16:46 得分 0
x_zing(阿浩) 的
select distinct name from (select * from products where star='1' order by sn desc ) t
sky_blue(老衲)的
select DISTINCT rtrim(ltrim(name)) from products where star='1' order by sn DESC
去掉order by sn DESC都可以执行,结果也正确,只是显示顺序就错了
我把order by sn DESC改成order by id DESC也不行
是什么原因呀
我以前用的语句没有order by sn DESC这个零件,哈哈,好痛呀!
Top
14 楼N_chow(Yukon)回复于 2002-03-28 15:17:59 得分 5
那麼費勁干嘛?!
把sn同時Select出來就好了,速度肯定不會比你單獨Select一列慢.(int型嘛)
select DISTINCT name,sn from products where star='1' order by sn DESCTop
15 楼Bullforg(...)回复于 2002-03-28 15:18:55 得分 0
哦,对不起二位,我看明白了,我该给分了
多谢!Top
16 楼N_chow(Yukon)回复于 2002-03-28 15:19:47 得分 0
From Books on line.
ORDER BY 子句可以包括未出現在選取清單中的項目。 但是,如果指定了 SELECT DISTINCT,或者 SELECT 陳述式含有 UNION 運算子,那麼排序資料行就必須出現在選取清單中。
此外,SELECT 陳述式中包含 UNION 運算子時,資料行名稱或資料行別名必須在第一選取清單中指定
Top
17 楼Bullforg(...)回复于 2002-03-28 15:28:53 得分 0
谢谢各位,其实只要是原有的程序千窗百孔,实在惨不忍睹。
我写的代码还有各位的代码在调试器中基本都能通过
可在程序中就是有错,不是这句代码的我问题,而是原先程序的错误
谢谢各位。
N_Chow(一劍飄香++) 也谢谢你,虽然你来晚了,但还是谢谢你
我的问题还会很多,还请各位帮忙Top




