BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / database / #2851同步于 2008/12/17
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖

[合集] [求助] 某SQL语句的写法

coolfantasy
2008/12/17镜像同步0 回复
☆─────────────────────────────────────☆ ATV (ATV) 于 (Mon Dec 1 17:10:45 2008) 提到: 数据库里的表是这样的: CREATE TABLE t1(a INTEGER, b FLOAT, c INTEGER, d INTEGER) 现在要查询 a = xxx 的记录中 d值最小的那条记录 如何写SQL语句更简洁呢? 我只想到select * from t1 where a = xxx and d = (select min(d) from t1 where a = xxx) 感觉很原始 ☆─────────────────────────────────────☆ IkariShinji (轉型|[路]團長|碇シンジ|The 3rd Child) 于 (Mon Dec 1 17:43:09 2008) 提到: 可以order一下么…… ☆─────────────────────────────────────☆ purevirtual (天之健|杨无敌|hf) 于 (Mon Dec 1 20:18:23 2008) 提到: select a, b, c, min(d) from t1 where a=xxx ☆─────────────────────────────────────☆ dickfu (潇湘天下|小付寨主) 于 (Mon Dec 1 21:03:26 2008) 提到: 你这明显会报语法错误嘛... ☆─────────────────────────────────────☆ dickfu (潇湘天下|小付寨主) 于 (Mon Dec 1 21:07:19 2008) 提到: 如果只要一个select可以这么考虑一下...但是是mysql的语法...不是通用sql select * from t1 where a=xxx order by d desc limit 1 ☆─────────────────────────────────────☆ xt9876 ([X|Z]Tao~~zZ) 于 (Mon Dec 1 21:24:45 2008) 提到: xiaofu~~ ☆─────────────────────────────────────☆ purevirtual (天之健|杨无敌|hf) 于 (Mon Dec 1 21:48:20 2008) 提到: 不啊 ☆─────────────────────────────────────☆ dickfu (潇湘天下|小付寨主) 于 (Mon Dec 1 21:55:31 2008) 提到: 额... ☆─────────────────────────────────────☆ FlyBeast (ah_jun) 于 (Mon Dec 1 23:36:02 2008) 提到: 【 在 purevirtual 的大作中提到: 】 请问你用的是什么数据库,我在我的本地的oracle和DB2上试了都报错 ☆─────────────────────────────────────☆ FlyBeast (ah_jun) 于 (Mon Dec 1 23:38:39 2008) 提到: 【 在 dickfu 的大作中提到: 】 这样是不错,但是如果最小d的有多条记录怎么办呢? ☆─────────────────────────────────────☆ lacrimosa (北邮鲨鱼帮--门板鲨鱼) 于 (Mon Dec 1 23:57:55 2008) 提到: 跟踪~,现在你还这么活跃~ 【 在 dickfu 的大作中提到: 】 ☆─────────────────────────────────────☆ FlyBeast (ah_jun) 于 (Tue Dec 2 00:20:50 2008) 提到: 如果最小的D没有多条记录的话,order by,然后取第一条 ok了,如果有多条的话我觉得这样可以实现,简洁是做不到了,但是效率可能会比楼主列出的稍高一点,至少不需要扫描t1表两次 select A,B,C,D FROM (SELECT A,B,C,D,RANK() OVER (ORDER BY D) RK from t1 where a = xxx) WHERE RK=1 RANK() OVER (ORDER BY D)可能不是标准的sql,我在oracle上试了下,没什么问题。 ☆─────────────────────────────────────☆ zxalh (我来自灾区) 于 (Tue Dec 2 00:29:54 2008) 提到: select * from t1 where t1.a=xxx order by 4 fetch first row only; --DB2 select * from (select * from t1 where t1.a=xxx order by 4 ) where rownum=1;--Oracle
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。