一个select的问题,请教各位。

pwair 2003-05-27 01:10:49
问题:
talbe:
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/03 | 10000
01 | 2003/04 | 20000
01 | 2003/05 | 30000
01 | 2003/06 | 40000
01 | 2003/07 | 20000
02 | 2003/02 | 20000
02 | 2003/03 | 20000
02 | 2003/04 | 20000
02 | 2003/05 | 30000
想把01和02中最新的update_date所对应的input_money选出来。
也就是:
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/07 | 20000
02 | 2003/05 | 30000

试过:
select a.company_code,a.input_money, b.update_date from test1 a,(select max(update_date) from test1 where test1.company_code = a.company_code) b
系统提示:
ERROR: Relation 'a' does not exist

还试过:
select t1.company_code, t1.update_date, t1.input_money from test1 as t1
where (company_code,update_date) in (select t2.company_code, max(t2.update_date) from test1 as t2 group by t2.company_code);
这个sql可以选出数据,可是如果数据量增大,运行速度会变慢很多。

请教各位有没有什么好的办法可以提高运行效率。
(以上的sql查询和结果都是在Postgresql下运行的)
...全文
145 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
pwair 2003-05-30
  • 打赏
  • 举报
回复
第一次发贴,结贴,谢谢各位的帮助。
tomboy0 2003-05-29
  • 打赏
  • 举报
回复
MySql不支持子查询,但支持left join
如果当前表(td_temp)与自己(td_temp)联立,数据量大时,sql语句的效率很低,数据库容易死掉。
Arbow 2003-05-29
  • 打赏
  • 举报
回复
"MySql不支持子查询,但支持left join"

4.0版本以上支持子查询
tomboy0 2003-05-29
  • 打赏
  • 举报
回复
楼主,该结贴了!
pwair 2003-05-28
  • 打赏
  • 举报
回复
SELECT company_code,max(update_date),input_money FROM test1 group by company_code,input_money;
运行结果:
company_code | max | input_money
--------------+------------+-------------
01 | 2003/03 | 10000
01 | 2003/07 | 20000
01 | 2003/05 | 30000
01 | 2003/06 | 40000
02 | 2003/04 | 20000
02 | 2003/05 | 30000
(6 rows)
结果不对。
Arbow 2003-05-28
  • 打赏
  • 举报
回复
楼上的,问题没有那么简单,用你的语句,查询出来的input_money数是错的。
Arbow 2003-05-28
  • 打赏
  • 举报
回复
:P

没有用过MySQL的子查询

既然可以用子查询,那么应该用left join也行的吧,那位高手给出一个语句?
homeness 2003-05-28
  • 打赏
  • 举报
回复
楼上得该说得都说了,
我帮你up
tomboy0 2003-05-28
  • 打赏
  • 举报
回复
上面写的不对!重写:
先建立一临时表:
create table td_temp1
select company_code,max(update_date) update_date
from td_temp
group by company_code;
再联立两个表:
select a.* from td_temp a,td_temp1 b
where a.company_code=b.company_code
and a.update_date=b.update_date
肯定行!
tomboy0 2003-05-28
  • 打赏
  • 举报
回复
select company_code,max(update_date),input_money
from td_temp
group by company_code;
我试过了,行!
pwair 2003-05-28
  • 打赏
  • 举报
回复
谢谢yzssg(秋枫) 的sql,运行速度真的快了很多。
谢谢lierq(李子) ,Arbow(◎_◎) ,swotcoder(苦 丁) ,neumqp(风之子) 对这个帖子的关注和参与。
如果各位还有更好的方法,或者别的什么方法,希望大家贴出来。
谢谢各位。
swotcoder 2003-05-28
  • 打赏
  • 举报
回复
是MySQL的语法,他用的子查询 ^_^
Arbow 2003-05-28
  • 打赏
  • 举报
回复
楼上的是PostgreSql的语法吗?
只懂MySQL,看不懂:P

yzssg 2003-05-28
  • 打赏
  • 举报
回复
select a.company_code,a.input_money, b.update_date from test1 a ,(select company_code,max(update_date) as update_date from test1 group by company_code) b where b.company_code = a.company_code and a.update_date=b.update_date
neumqp 2003-05-27
  • 打赏
  • 举报
回复
SELECT company_code,max(update_date),input_money
FROM test1 group by company_code;
Arbow 2003-05-27
  • 打赏
  • 举报
回复
关注。。。想了好久都想不出。。
lierq 2003-05-27
  • 打赏
  • 举报
回复
MYSQL用4.1,可以支持子查询

57,062

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧