试图正确获取行位置

时间:2017-04-16 16:35:20

标签: php mysql

我目前have a question about this但我认为它没有得到应有的重视,可能是因为它在整个时间内都有扩展和版本。有了这个,我会尽量简洁,放手吧!

我想在这个SQLFiddle

中获取特定表的行位置

我目前有2个查询:一个列出按列排序排序的所有行(其他行的COUNT());和其他获取特定行的信息。

在SQLFiddle的这个屏幕截图中,我有当前的结果,并且以黄色(位置列旁边)我有所需的结果。您可以看到“position”可能是指列ID,而不是列级别,因为它应该是:

Current results / Desired result (in yellow)

如何达到预期效果?

如果Fiddle没有加载,这里是所有的DLL:

架构:

CREATE TABLE IF NOT EXISTS `site_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `site_users` (`id`, `username`) VALUES
(1, 'Mateus'),
(2, 'Marco'),
(3, 'John'),
(4, 'Luke'),
(5, 'Wall');

CREATE TABLE IF NOT EXISTS `site_articles_comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `data` int(11) NOT NULL,
  `comment` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

INSERT INTO `site_articles_comments` (`id`, `user_id`, `data`, `comment`) VALUES
(1, 1, 1492101324, 'Too good to be true'),
(2, 1, 1492101392, 'It\'s so, not very.'),
(3, 1, 1492106262, 'I loved it!'),
(4, 3, 1492107619, 'Seems fishy'),
(5, 1, 1492108002, 'Don\'t blame it on her'),
(6, 2, 1492108433, 'As I highly doubt it'),
(7, 2, 1492200402, 'This is just a test'),
(8, 2, 1492267629, 'Another test'),
(9, 2, 1492267684, 'No sht'),
(10, 4, 1492267766, 'WTF?'),
(11, 2, 1492267796, 'Pics, pleaseee!');

测试查询:

/* This simply order users on rank (rank being number of comments) */
/* This query tries to get the row position of user based on rank */

SELECT x.id,
       x.username,
       x.rank,
       x.position
  FROM (SELECT u.id,
               u.username,
               @rownum := @rownum + 1 AS position,
               (SELECT COALESCE(COUNT(DISTINCT(c.id)),0) AS rank
                  FROM site_articles_comments c
                  WHERE u.id = c.user_id ORDER BY rank DESC) AS rank
          FROM site_users u
          JOIN (SELECT @rownum := 0) r
        ORDER BY rank DESC) x;

/* Based on the list above, user (Marco) should have a position number of 1 */

SELECT x.id,
       x.username,
       x.rank,
       x.position
  FROM (SELECT u.id,
               u.username,
               @rownum := @rownum + 1 AS position,
               (SELECT COALESCE(COUNT(DISTINCT(c.id)),0) AS rank
                  FROM site_articles_comments c
                  WHERE u.id = c.user_id ORDER BY rank DESC) AS rank
          FROM site_users u
          JOIN (SELECT @rownum := 0) r
        ORDER BY rank DESC) x
  WHERE x.id = 2

1 个答案:

答案 0 :(得分:0)

您正在寻找的可能是:

SET @rowno2=0;

SELECT x.id,
       x.username,
       x.rank,
       x.position,
       @rowno2:=@rowno2+1 AS rowno
  FROM (SELECT u.id,
               u.username,
               @rownum := @rownum + 1 AS position,
               (SELECT COALESCE(COUNT(DISTINCT(c.id)),0) AS rank
                  FROM site_articles_comments c
                  WHERE u.id = c.user_id ORDER BY rank DESC) AS rank
          FROM site_users u
          JOIN (SELECT @rownum := 0) r
        ORDER BY rank DESC) x;

SET @rowno2=0;

SELECT x.id,
       x.username,
       x.rank,
       x.position,
       @rowno2:=@rowno2+1 AS rowno
  FROM (SELECT u.id,
               u.username,
               @rownum := @rownum + 1 AS position,
               (SELECT COALESCE(COUNT(DISTINCT(c.id)),0) AS rank
                  FROM site_articles_comments c
                  WHERE u.id = c.user_id ORDER BY rank DESC) AS rank
          FROM site_users u
          JOIN (SELECT @rownum := 0) r
        ORDER BY rank DESC) x
  WHERE x.id = 2

因此,您可以使用MySQL变量来跟踪行号。

我没有看过你的其余SQL。