mysql – 在ORDER BY之后计算记录

前端之家收集整理的这篇文章主要介绍了mysql – 在ORDER BY之后计算记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有代码来计算记录,但无法在它之前添加订单.

连接了两个表,我添加代码来计算记录.问题是我想首先ORDER BY SN并在之后分配cnt?

我的代码是:

  1. create table rot (
  2. code int(10) primary key,PN varchar(10) not null,SN varchar(10) not null,LocID int(10) not null);
  3. insert into rot values (1,'T1','T1SN1','1');
  4. insert into rot values (2,'A1','A1SN1','2');
  5. insert into rot values (3,'J1','J1SN1','3');
  6. insert into rot values (4,'A2','A2SN1','1');
  7. insert into rot values (5,'J2','J2SN1','2');
  8. insert into rot values (6,'A3','A3SN1','3');
  9. insert into rot values (7,'J3','J3SN1','4');
  10. insert into rot values (8,'T1SN2','5');
  11. insert into rot values (9,'A1SN2','1');
  12. insert into rot values (10,'J2SN2','3');
  13. insert into rot values (11,'J2SN3','4');
  14. insert into rot values (12,'A1SN3','3');
  15. insert into rot values (13,'J2SN4','5');
  16. create table loc(
  17. code1 int(10) primary key,LocVar varchar(10) not null);
  18. insert into loc values (1,'AAA');
  19. insert into loc values (2,'BBB');
  20. insert into loc values (3,'CCC');
  21. insert into loc values (4,'DDD');
  22. insert into loc values (5,'EEE');

Cnt代码

  1. SELECT * FROM rot
  2. JOIN loc ON rot.code = loc.code1
  3. JOIN (
  4. SELECT t1.code,count(*) cnt FROM (
  5. SELECT distinct code
  6. FROM rot ts1
  7. JOIN loc tx1 ON ts1.code = tx1.code1
  8. ) t1
  9. JOIN (
  10. SELECT distinct code
  11. FROM rot ts2
  12. JOIN loc tx2 ON ts2.code = tx2.code1
  13. ) t2 on t1.code <= t2.code
  14. group by t1.code
  15. ) tt ON rot.code = tt.code

结果:

  1. +------+----+-------+-------+-------+--------+------+-----+
  2. | code | PN | SN | LocID | code1 | LocVar | code | cnt |
  3. +------+----+-------+-------+-------+--------+------+-----+
  4. | 2 | A1 | A1SN1 | 2 | 2 | BBB | 2 | 4 |
  5. | 4 | A2 | A2SN1 | 1 | 4 | DDD | 4 | 2 |
  6. | 3 | J1 | J1SN1 | 3 | 3 | CCC | 3 | 3 |
  7. | 5 | J2 | J2SN1 | 2 | 5 | EEE | 5 | 1 |
  8. | 1 | T1 | T1SN1 | 1 | 1 | AAA | 1 | 5 |
  9. +------+----+-------+-------+-------+--------+------+-----+

期望的结果

  1. +------+----+-------+-------+-------+--------+------+-----+
  2. | code | PN | SN | LocID | code1 | LocVar | code | cnt |
  3. +------+----+-------+-------+-------+--------+------+-----+
  4. | 2 | A1 | A1SN1 | 2 | 2 | BBB | 2 | 1 |
  5. | 4 | A2 | A2SN1 | 1 | 4 | DDD | 4 | 2 |
  6. | 3 | J1 | J1SN1 | 3 | 3 | CCC | 3 | 3 |
  7. | 5 | J2 | J2SN1 | 2 | 5 | EEE | 5 | 4 |
  8. | 1 | T1 | T1SN1 | 1 | 1 | AAA | 1 | 5 |
  9. +------+----+-------+-------+-------+--------+------+-----+

我只是想知道在哪里放置ORDER BY?在我的代码中,我无法分配变量,代码必须以SELECT开头.

最佳答案
根据我的理解,并根据我对你的问题所作的评论,我将引用下一篇

In the Results table and the logic of the query you have made,the
cnt column keep a counter of the number of codes that are greater or equal compared to the code value of the row. In other words,and
for example,code 2 is lower or equal than codes 2,3,4 and 5,so you
store a 4 on the cnt column. But,in the Desired Results this
has lost all sense,since you only save the position of the current
ordering on the cnt column.

假设您只需要在cnt列上使用SN的订单位置,您可以尝试下一个不依赖于MysqL 8.0并且不使用用户变量的解决方案:

  1. SELECT rot.*,loc.*,( SELECT COUNT(*)
  2. FROM rot AS rot1
  3. INNER JOIN loc AS loc1 ON loc1.code1 = rot1.code
  4. WHERE rot1.SN <= rot.SN ) AS cnt
  5. FROM
  6. rot
  7. INNER JOIN
  8. loc ON loc.code1 = rot.code
  9. ORDER BY
  10. rot.SN

猜你在找的MySQL相关文章