求sql语句

byeceg 2012-02-20 03:51:55
表结构 table A
id title xx xx date identityid


select A.id from A where A.identityid is not null order by A.date

由于identityid字段可能有null值,也可能有重复的值。空值我用了is not null;重复的值使用group by 会导致order by无效。如果使用 distionct A.identityid 返回的值就不是id了。
请问下,如果不用子查询,有什么好的办法吗?
...全文
127 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2012-02-21
  • 打赏
  • 举报
回复
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

WWWWA 2012-02-21
  • 打赏
  • 举报
回复
select a.* from tt a inner join
(select identityid,max(credate) as ma from tt where identityid is not null group by identityid ) b
on a.identityid=b.identityid and a.credate=ma
oO寒枫Oo 2012-02-21
  • 打赏
  • 举报
回复
row_number(partition by identityid order by credate desc)
oO寒枫Oo 2012-02-21
  • 打赏
  • 举报
回复
要是mysql有 分组排序函数就简单了
可以自己写一个
用子查询的话 10楼的很好了、
WWWWA 2012-02-21
  • 打赏
  • 举报
回复



SELECT * FROM TTH A WHERE identityid is not null AND NOT EXISTS(SELECT 1 FROM TTH WHERE A.identityid=identityid AND A.credate<credate)
byeceg 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 acmain_chm 的回复:]

引用 6 楼 byeceg 的回复:

引用 5 楼 acmain_chm 的回复:

select A.id ,A.date
from A
where A.identityid is not null
group by A.id
order by A.date

谢谢.
不过这样没有去除重复的。


请贴出用了上面语句后仍然重复的例子
[/Quote]
先谢谢了。

比如表是这样的。

id identityid credate memo
1 1 2011-01-02 00:00:00 test1
2 1 2011-01-01 00:00:00 test2
3 1 2011-01-21 00:00:00 test3
4 2 2011-01-01 00:00:00 test4
5 2 2011-02-01 00:00:00 test5
6 2 2011-01-01 00:00:00 test6
7 3 2011-01-11 00:00:00 test7
8 3 2011-01-08 00:00:00 test8
9 null 2011-01-01 00:00:00 test9
10 null 2011-01-01 00:00:00 test10
11 null 2011-01-01 00:00:00 test11

我需要的结果是:
id 3
id 5
id 7
现在sql语句也写出来了:
select SUBSTRING_INDEX(group_concat(t.id order by t.credate desc),',',1) from test as t
where t.identityid is not null group by t.identityid
上面可以满足我上面的需求。但是在hibernate里不能执行。不知道还有没有其它的写法?
ACMAIN_CHM 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 byeceg 的回复:]

引用 5 楼 acmain_chm 的回复:

select A.id ,A.date
from A
where A.identityid is not null
group by A.id
order by A.date

谢谢.
不过这样没有去除重复的。
[/Quote]

请贴出用了上面语句后仍然重复的例子
oO寒枫Oo 2012-02-20
  • 打赏
  • 举报
回复

select A.id ,min(A.date)
from A
where A.identityid is not null
group by A.id
order by 2
byeceg 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acmain_chm 的回复:]

select A.id ,A.date
from A
where A.identityid is not null
group by A.id
order by A.date
[/Quote]
谢谢.
不过这样没有去除重复的。
ACMAIN_CHM 2012-02-20
  • 打赏
  • 举报
回复
select A.id ,A.date
from A
where A.identityid is not null
group by A.id
order by A.date
wwwwb 2012-02-20
  • 打赏
  • 举报
回复
贴建表及插入记录的SQL,及要求结果出来看看

估计可以用连接解决
byeceg 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwwb 的回复:]

详细说明要求
假设ID唯一
select A1.id from A a1 where A1.identityid is not null
and not exists(select 1 from a where a1.identityid=identityid and a1.id<>id)
order by A1.date
[/Quote]
谢谢。只能用子查询吗?

select A.id from A where A.identityid is not null group by A.identityid  order by A.date desc

这样写 order by 就无效了
oO寒枫Oo 2012-02-20
  • 打赏
  • 举报
回复

1 2号
1 3号
2 1号
2 4号


这样的表 你得到的记录到底是
1
2

还是
2
1
wwwwb 2012-02-20
  • 打赏
  • 举报
回复
详细说明要求
假设ID唯一
select A1.id from A a1 where A1.identityid is not null
and not exists(select 1 from a where a1.identityid=identityid and a1.id<>id)
order by A1.date

56,677

社区成员

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

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