(sql)如何获取队列中条目的编号?

bkhjykvo  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(273)

我有这张table:

CREATE TABLE tickets (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
topic VARCHAR(64) NOT NULL,
priority TINYINT NOT NULL default 1,
created DATETIME); 

INSERT INTO tickets (id, topic, priority, created) VALUES
(1, 'kin23oi5n2', 3, '2018-05-27 16:00:54'),
(2, 'ewbrv32b3', 2, '2018-05-27 16:00:54'),
(3, 'aern23523', 1, '2018-05-27 16:01:15'),
(4, '4bt43t3', 5, '2018-05-27 16:01:15'),
(5, '32n523n5235n235t3n4', 4, '2018-05-27 16:01:39'),
(6, 'e2nr2n23n5235n23n523', 3, '2018-05-27 16:01:39');

我正在运行一个查询:

SELECT * FROM `tickets` order by `priority` ASC, `created` desc;

如何使用sql获取此结果中id为5的行号?
更新: SELECT count(*) as pos FROM 门票 WHERE ...; 因此,我们得到 pos 我试图使我的结果与此类似,但包含了必要行的行数(例如ID5)。
换句话说,我想得到队列条目的编号

wbgh16ku

wbgh16ku1#

如果有人感兴趣,下面是答案

SELECT num FROM

(SET @row_number = 0;

SELECT 
    (@row_number:=@row_number + 1) AS num, id
FROM
    tickets order by `priority` ASC, `created` desc) as tmp_table WHERE id = 6;

但现在还有一个问题。如何在larabel5.6中实现这一点?
他站起来做了。

相关问题