我有以下2个表格:
create table1
(
SENDER int,
RECEIVER int,
TIME time,
TYPE char(1)
);
create table2
(
ID int,
Y int,
CONTACT int,
DATE time
);
我正在执行以下连接查询:
SELECT B.ID, A.RECEIVER AS Z, A.SENDER AS CONTACT, A.TYPE, A.TIME
FROM table1 A
JOIN table2 B ON A.RECEIVER = B.CONTACT
WHERE A.TYPE = 'A'
AND A.TIME < B.DATE
如何使用(ID,CONTACT)
修改查询以仅返回每个GROUP BY
对的前40个结果?
我可以使用字段table2.DATE
答案 0 :(得分:0)
因为我想要每个ID的前40个结果,我将ID,autoId作为主键,这里autoId是一个自动增量键。所以执行以下查询后:
SELECT B.ID, A.RECEIVER AS Z, A.SENDER AS CONTACT, A.TYPE, A.TIME
FROM table1 A
JOIN table2 B ON A.RECEIVER = B.CONTACT
WHERE A.TYPE = 'A'
AND A.TIME < B.DATE
我得到的结果是,autoId为每个ID初始化为1
例如:
ID CONTACT autoId
1 2 1
1 3 2
1 11 3
1 34 4
2 5 1
2 33 2
2 56 3
因为autoId是自动增量,所以已经有一个索引。创建此表后,我可以轻松删除autoId大于40的结果。这个过程运行得非常快!