查询问题的加分再问
1楼: http://www.delphibbs.com/delphibbs/dispq.asp?lid=3040603
字串2
2楼: 问题:查询语句的问题: ( 积分:20, 回复:13, 阅读:104 )
分类:数据库-文件型 ( 版主:hbezwwl, bubble )
来自:zzz_222, 时间:2005-4-12 9:26:00, ID:3040603 | 编辑 [显示:小字体 | 大字体]
AQ1.SQL.Add(‘select distinct 处理项目 from 数据处理 where 批号=:p‘);
我想让其按编号顺序排,但又不能加上order by 编号
该如何写才能达到我的目的?
来自:zzz_222, 时间:2005-4-12 9:43:01, ID:3040635 | 编辑
AQ1.SQL.Add(‘select distinct 处理项目 from 数据处理 where 批号=:p order by 编号‘);distinct与order by 冲突
来自:twos, 时间:2005-4-12 9:45:16, ID:3040641
什么数据库?支持嵌套查询吗?
如果不支持嵌套,还是就只能分两步:
首先,select 处理项目,编号 into temptable from 数据处理 where 批号=... order by 编号
也就是把查询结果弄到一个临时表里面去
然后,从临时表里面查出你要的内容
来自:zzz_222, 时间:2005-4-12 9:55:46, ID:3040662 | 编辑
表结构:
处理项目 编号 批号
AA 1 1
AA 2 1
CC 3 1
CC 4 1
BB 5 1
AA 6 1
.......................
AQ1.SQL.Add(‘select distinct 处理项目 from 数据处理 where 批号=:p‘);
会得出:
处理项目 编号 批号
AA 1 1
BB 5 1
CC 3 1
...........................
而我想得到:
处理项目 编号 批号
AA 1 1
CC 3 1
BB 5 1
............................
该如何写代码?
来自:wjava, 时间:2005-4-12 11:57:54, ID:3040964
可以采用控件解决,肯定可以实现
来自:Begin_End, 时间:2005-4-12 12:03:38, ID:3040981
varstr1=‘select distinct 处理项目 from 数据处理 where 批号=:p ‘
varstr2=‘ select * from (varstr1) order by 编号 ‘
AQ1.SQL.Add(varstr2)
可否?
来自:zzz_222, 时间:2005-4-12 14:24:53, ID:3041241 | 编辑
varstr1:=‘select distinct 处理项目 from 数据处理 where 批号=:p ‘
varstr2:=‘ select * from (varstr1) order by 编号 ‘
AQ1.SQL.Add(varstr2)
不行啊
来自:twos, 时间:2005-4-12 14:34:03, ID:3041276
如果数据库支持嵌套,上面的语句应该是可以的
如果不支持,就不行
你可以使用临时表的办法,呵呵
来自:zzz_222, 时间:2005-4-12 15:02:08, ID:3041313 | 编辑
数据库是access,
dm.AQ1.Close;
dm.AQ1.SQL.Clear;
dm.AQ1.SQL.Add(‘select * from (select distinct 处理项目 from 数据处理 where 批号=:p) order by 编号‘);
dm.AQ1.Parameters.ParamByName(‘p‘).Value:=ss1;
dm.AQ1.Open;
运行报错:至少一个参数没有被指定值
注意:select distinct 处理项目 from 数据处理 where 批号=:p
只能得到处理项目这一项,后面order by 编号怎么可能查得到?
来自:wjava, 时间:2005-4-12 17:32:31, ID:3041726
在Access中是支持以上语句的,我在Access2000中以下语句通过:
SELECT * FROM Test050303
WHERE Test050303.wpdm in (SELECT DISTINCT Test050303.wpdm FROM Test050303 WHERE Test050303.wpdm like ‘M05*‘ ORDER BY Test050303.wpdm);
由此认为:可能是Query1的问题,是否把Query删除,采用AdoQuery再试试
来自:qinjlin76, 时间:2005-4-12 19:46:34, ID:3041871
用AdoQuery
来自:newsmile, 时间:2005-4-13 7:28:52, ID:3042105
dm.AQ1.SQL.Add(‘select * from (select distinct 处理项目 from 数据处理 where 批号=:p) order by 编号‘);
dm.AQ1.Parameters.ParamByName(‘p‘).Value:=ss1;
这两行改成
sqltxt:=‘select * from (select distinct 处理项目 from 数据处理 where 批号=‘+p+‘ order by 编号‘);//sqltxt:string
dm.AQ1.SQL.Add(sqltxt);
来自:zzz_222, 时间:2005-4-15 9:06:00, ID:3045217 | 编辑
首先我的AQ1就是adoquery1,其次distinct与order by 语句有冲突,我的程序也支持嵌套语句,只是都不行啊
表结构:
处理项目 编号 批号
AA 1 1
AA 2 1
CC 3 1
CC 4 1
BB 5 1
AA 6 1
.......................
AQ1.SQL.Add(‘select distinct 处理项目 from 数据处理 where 批号=:p‘);
会得出:
处理项目 | 编号 批号
AA | 1 1
BB | 5 1
CC | 3 1
.........|..................竖线这边的是对应的其它字段内容
而我想得到:
处理项目 | 编号 批号
AA | 1 1
CC | 3 1
BB | 5 1
.........|...................
来自:引力, 时间:2005-4-15 9:54:43, ID:3045267
SQL Server下测试通过:
SELECT DISTINCT 处理项目, MIN(编号) AS 编号, 批号
FROM 数据处理
GROUP BY 处理项目, 批号
HAVING (批号 = 1)
ORDER BY 编号 字串5
3楼: 引力,的回答在MySql下也能通过 字串5 4楼: 数据库:[red]Access200[/red] 字串4
5楼: ‘select distinct 处理项目 from 数据处理 where 批号=:p order by 编号‘ 在MySql下也可以通过,得出正确的结果 字串9 6楼: 在ACCESS数据库下不支持这种嘛,‘select distinct 处理项目 from 数据处理 where 批号=:p order by 编号‘ 会说distinct与order by 编号 冲突字串5
7楼: 这样应该是可以的,你试试: select distinct a.处理项目 from (select distinct 处理项目 as 处理项目,编号 from 数据处理 where 批号=:p order by 编号) a 字串7 8楼: select distinct a.处理项目 from (select distinct 处理项目 as 处理项目,编号 from 数据处理 where 批号=:p order by 编号) a 这样查询出来的数据跟 AQ1.SQL.Add(‘select distinct 处理项目 from 数据处理 where 批号=:p‘); 查询出来的一样,都是按照处理项目的顺序来排的字串4
9楼: access中加了distinct后结果集是只读的,所以不能order by了。你可以先用order by查询再用distinct查询。 字串3 10楼: 我要按order by 后面的排序,按distinct的查 字串3