mysql-count order-by之后的记录

2ekbmq32  于 2021-06-18  发布在  Mysql
关注(0)|答案(5)|浏览(349)

我有代码来计数记录,但无法添加之前的顺序。
两个表连接在一起,我添加了代码来统计记录。问题是我想先按序列号订购,然后再分配cnt?
我的代码是:
table

create table rot (
            code int(10) primary key,
            PN varchar(10) not null,
        SN varchar(10) not null,
            LocID int(10) not null);

    insert into rot values (1,'T1','T1SN1','1');
    insert into rot values (2,'A1','A1SN1','2');
    insert into rot values (3,'J1','J1SN1','3');
    insert into rot values (4,'A2','A2SN1','1');
    insert into rot values (5,'J2','J2SN1','2');
    insert into rot values (6,'A3','A3SN1','3');
    insert into rot values (7,'J3','J3SN1','4');
    insert into rot values (8,'T1','T1SN2','5');
    insert into rot values (9,'A1','A1SN2','1');
    insert into rot values (10,'J2','J2SN2','3');
    insert into rot values (11,'J2','J2SN3','4');
    insert into rot values (12,'A1','A1SN3','3');
    insert into rot values (13,'J2','J2SN4','5');

    create table loc(
        code1 int(10) primary key,
        LocVar varchar(10) not null);

    insert into loc values (1,'AAA');
    insert into loc values (2,'BBB');
    insert into loc values (3,'CCC');
    insert into loc values (4,'DDD');
    insert into loc values (5,'EEE');

cnt代码:

SELECT * FROM rot
JOIN loc ON rot.code = loc.code1

JOIN (
            SELECT t1.code, count(*) cnt FROM (
                SELECT distinct code
                FROM rot ts1 
            JOIN loc tx1 ON ts1.code = tx1.code1 

) t1 
JOIN (
    SELECT distinct code
                FROM rot ts2
            JOIN loc tx2 ON ts2.code = tx2.code1

    ) t2 on t1.code <= t2.code 
    group by t1.code 

) tt ON rot.code = tt.code

结果:

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

期望的结果

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

我只是想知道在哪里下单?在我的代码中,我无法分配变量,代码必须以select开头。

oug3syen

oug3syen1#

试试这个:

SELECT * FROM (
    SELECT *
    FROM rot
    JOIN loc ON rot.code = loc.code1
    JOIN (
        SELECT t1.code3, count(*) cnt
        FROM (
            SELECT distinct code as code3
            FROM rot ts1
            JOIN loc tx1 ON ts1.code = tx1.code1
        ) t1
        JOIN (
            SELECT distinct code
            FROM rot ts2
            JOIN loc tx2 ON ts2.code = tx2.code1
        ) t2 on t1.code3 <= t2.code group by t1.code3
    ) tt ON rot.code = tt.code3
)X
ORDER BY X.cnt ASC;
gstyhher

gstyhher2#

如果mysql 8.0你可以使用 ROW_NUMBER :

SELECT *, rot.code, ROW_NUMBER() OVER(ORDER BY SN) AS cnt
FROM rot
JOIN loc ON rot.code = loc.code1
ORDER BY SN;

db<>小提琴演示

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

nc1teljy3#

SELECT code, PN, SN, LocID, code1, LocVar, code, @rownum:=@rownum + 1 as cnt FROM rot
JOIN loc ON rot.code = loc.code1
JOIN (
    SELECT distinct code
                FROM rot ts2
            JOIN loc tx2 ON ts2.code = tx2.code1

    ) t2 on t1.code <= t2.code 
    group by loc.code1 
    order by rot.SN;

这就是你想要的吗?

yrdbyhpb

yrdbyhpb4#

根据我的理解,并根据我对你的问题所作的评论,我接下来将引用:
在results表和所做查询的逻辑中,cnt列保留一个与行的代码值相比大于或等于的代码数计数器。换句话说,例如,代码2小于或等于代码2、3、4和5,所以在cnt列上存储一个4。但是,在期望的结果中,这已经失去了所有意义,因为您只保存cnt列上当前排序的位置。
假设您只需要cnt列上的order position by sn,您可以尝试下一个不依赖mysql 8.0且不使用用户变量的解决方案:

SELECT rot.*,
       loc.*,
       ( SELECT COUNT(*)
         FROM rot AS rot1
         INNER JOIN loc AS loc1 ON loc1.code1 = rot1.code
         WHERE rot1.SN <= rot.SN ) AS cnt
FROM
    rot
INNER JOIN
    loc ON loc.code1 = rot.code
ORDER BY
    rot.SN
bihw5rsg

bihw5rsg5#

根据以上给定的详细信息,您可以使用以下查询获得所需的结果:

SELECT code, PN,SN,LocID,code1,LocVar ,code, @row := @row + 1 AS cnt FROM (
SELECT  code, PN,SN,LocID,code1,LocVar
FROM rot 
JOIN loc 
ON code=code1
ORDER BY SN) tab, (SELECT @row := 0) r;

期望结果:

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

希望您能通过查询实现这一点。请检查并享受:)!

相关问题