按多个标准分组

时间:2017-02-26 23:27:54

标签: mysql group-by left-join

我已经尝试在Sql Fiddle上构建它,但我根本没有设法在其上运行简单的SELECT *查询。 我有以下查询,它根据标准的多重排序返回用户的个人资料照片。

  1. 被标记为相册中的主要照片
  2. 嵌套树(lft)中的专辑位置
  3. 用户在相册中定义的排序位置
  4. 照片中用户定义的排序位置
  5. 我设法为给定用户执行此操作,但我无法查询users表LEFT JOINING以下查询,以便拥有一个其他列,其中包含个人资料照片ID作为值。 如果我不必使用LIMIT 1那将是很容易的。

    / *以下查询获取给定用户最重要的文件* /

    SELECT f.*
         , c.lft
         , c.rgt
      FROM
         ( SELECT a.* 
                , b.catID
                , b.main
                , b.sort bsort
                , a.sort asort
             FROM foto a
             LEFT 
             JOIN foto_cats b 
               ON b.fotoID = a.id
            WHERE a.user = 79748
         ) f
      LEFT 
      JOIN cats c 
        ON f.catID = c.id
     WHERE f.delRequest = 0
     ORDER 
        BY main DESC
         , lft ASC
         , bsort ASC
         , asort ASC 
     LIMIT 1
    

    / *数据库架构* /

    CREATE TABLE IF NOT EXISTS `cats` (
      `tree_id` int(10) unsigned NOT NULL,
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `lft` int(11) NOT NULL,
      `rgt` int(11) NOT NULL,
      `name` varchar(100) NOT NULL,
      `system` tinyint(1) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `tree_id` (`tree_id`),
      KEY `lft` (`lft`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=119 ;
    
    
    
    INSERT INTO `cats` (`tree_id`, `id`, `lft`, `rgt`, `name`, `system`) VALUES
    (1, 1, 1, 232, 'Category name2', 0),
    (1, 2, 2, 23, 'Category name 3', 0),
    (1, 3, 3, 4, 'Category name 4', 1),
    (1, 4, 5, 6, 'Actor', 1),
    (1, 5, 7, 8, 'Modeling', 1),
    (1, 6, 9, 10, 'Modeling Composites', 1),
    (1, 7, 15, 16, 'Category name t', 1),
    (1, 8, 13, 14, 'Business', 1),
    (1, 10, 19, 20, 'Private', 1),
    (1, 11, 21, 22, 'Personale', 1),
    (1, 116, 11, 12, 'Hosting', 1);
    
    -- --------------------------------------------------------
    
    
    
    CREATE TABLE IF NOT EXISTS `foto` (
      `id` bigint(9) unsigned NOT NULL AUTO_INCREMENT,
      `file` varchar(100) NOT NULL,
      `user` bigint(10) NOT NULL,
      `taken` year(4) NOT NULL,
      `time` date NOT NULL,
      `sort` int(3) NOT NULL,
      `delRequest` varchar(10) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      KEY `poza` (`file`),
      KEY `user` (`user`),
      KEY `time` (`time`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=364411 ;
    
    
    
    INSERT INTO `foto` (`id`, `file`, `user`, `taken`, `time`, `sort`, `delRequest`) VALUES
    (357292, '79748_323722.jpg', 79748, 0000, '2016-12-01', 9, '79748'),
    (357929, '79748_661469.jpeg', 79748, 0000, '2016-12-20', 3, '0'),
    (357933, '79748_699272.jpg', 79748, 0000, '2016-12-20', 8, '79748'),
    (357934, '79748_239191.jpg', 79748, 0000, '2016-12-20', 6, '1'),
    (357935, '79748_426873.jpg', 79748, 0000, '2016-12-20', 0, '0'),
    (357936, '79748_513276.jpg', 79748, 0000, '2016-12-20', 10, '79748'),
    (357937, '79748_183148.jpg', 79748, 0000, '2016-12-20', 7, '79748'),
    (357939, '79748_237699.jpg', 79748, 0000, '2016-12-20', 1, '1'),
    (357940, '79748_552583.jpg', 79748, 0000, '2016-12-20', 2, '0'),
    (362415, '79748_299345.jpg', 79748, 0000, '2017-02-13', 5, '1'),
    (362426, '79748_455975.jpg', 79748, 0000, '2017-02-13', 4, '1'),
    (364127, '79748_158293.jpg', 79748, 0000, '2017-02-25', 0, '0'),
    (364137, '79748_786692.jpg', 79748, 0000, '2017-02-25', 0, '0'),
    (364138, '79748_136966.jpg', 79748, 0000, '2017-02-25', 0, '0'),
    (364139, '79748_694159.jpg', 79748, 0000, '2017-02-25', 0, '0'),
    (364140, '79748_279553.jpg', 79748, 0000, '2017-02-25', 0, '0');
    
    
    
    CREATE TABLE IF NOT EXISTS `foto_cats` (
      `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
      `fotoID` bigint(10) NOT NULL,
      `catID` bigint(10) NOT NULL,
      `userID` bigint(10) NOT NULL,
      `sort` bigint(3) NOT NULL,
      `main` tinyint(1) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `user` (`userID`),
      KEY `pozaID` (`fotoID`)
    );
    
    
    
    INSERT INTO `foto_cats` (`id`, `fotoID`, `catID`, `userID`, `sort`, `main`) VALUES
    (252842, 357933, 3, 79748, 0, 0),
    (252575, 357933, 116, 79748, 0, 0),
    (252843, 357935, 3, 79748, 2, 0),
    (252578, 357934, 3, 79748, 0, 0),
    (252580, 357937, 3, 79748, 0, 0),
    (252581, 357936, 3, 79748, 0, 0),
    (252832, 357292, 8, 79748, 0, 0),
    (252833, 357933, 8, 79748, 0, 0),
    (252836, 357929, 8, 79748, 1, 0),
    (252845, 357292, 7, 79748, 0, 0),
    (252864, 357933, 10, 79748, 0, 0),
    (252888, 357929, 10, 79748, 0, 0),
    (252889, 357935, 10, 79748, 1, 0),
    (252891, 357940, 10, 79748, 2, 0),
    (252897, 357935, 8, 79748, 0, 0),
    (252902, 357940, 3, 79748, 0, 0),
    (253029, 357929, 3, 79748, 1, 0);
    
    
    
    CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) NOT NULL,
      `name` varchar(60) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    
    INSERT INTO `users` (`id`, `name`) VALUES
    (79748, 'John Smith');
    

    我想要查询用户表以获得结果

    +-------+---------------------+------------------+
    |  id   |        name         |     filemane     |
    +-------+---------------------+------------------+
    | 79748 | John Smith          | 79748_552583.jpg |
    |     3 | another user's name | that user's file |
    +-------+---------------------+------------------+
    

    由于显而易见的原因,我没有再插入用户。

    http://sqlfiddle.com/#!9/b6b98/2/0

0 个答案:

没有答案