复杂查询

So_CooL 2011-10-24 08:30:53
table 表中有一个字段id,event_id,event_type值:
id,event_id,event_type
1,1,1
2,7,2
3,1,2
4,1,3
5,7,2
6,2,3
7,3,5
查询出来7,2和他下面的一条记录,结果如下:
2,7,2
3,1,2
5,7,2
6,2,3
...全文
58 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
WWWWA 2011-10-24
  • 打赏
  • 举报
回复
在event_id、event_type、ID上建立复合索引
So_CooL 2011-10-24
  • 打赏
  • 举报
回复
查询速度好慢,有没有快一点的查询方法
WWWWA 2011-10-24
  • 打赏
  • 举报
回复
OR

SELECT * FROM ttw a WHERE
(a.`event_id`=7 AND a.`event_type`=2)
OR
EXISTS (SELECT 1 FROM ttw WHERE a.id=`id`-1 AND `event_id`<>7 AND `event_type`<>2 )
;
WWWWA 2011-10-24
  • 打赏
  • 举报
回复
加入自增ID
SELECT * FROM ttw a WHERE a.`event_id`=7 AND a.`event_type`=2
UNION
SELECT b.* FROM ttw a
LEFT JOIN ttw b ON (a.id=b.`id`-1 )
WHERE a.`event_id`=7 AND a.`event_type`=2 ORDER BY ID;
ACMAIN_CHM 2011-10-24
  • 打赏
  • 举报
回复
select * from table表
Where event_id in (7,2)
or id in (Select id+1 From table表 where event_id in (7,2))

56,679

社区成员

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

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