• Mysql 伪列及其应用


      伪列,顾名思义就是假的意思假列,不是真实存在的字段,有时候我们会对查询结果进行排序或者进行编号这时候我们就可以用到伪列了。mysql中定义伪列有两种方式,一种是将伪列的定义和使用分开,还有一种是使用和定义都放在一起两者通过全连联系起来,后者更加方便使用。

    用到的表 crm_user_yhq_result_copy1

    /*
     Navicat Premium Data Transfer
    
     Source Server         : 本地
     Source Server Type    : MySQL
     Source Server Version : 50726
     Source Host           : localhost:3306
     Source Schema         : framework
    
     Target Server Type    : MySQL
     Target Server Version : 50726
     File Encoding         : 65001
    
     Date: 09/05/2019 23:51:37
    */
    
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    -- ----------------------------
    -- Table structure for crm_user_yhq_result_copy1
    -- ----------------------------
    DROP TABLE IF EXISTS `crm_user_yhq_result_copy1`;
    CREATE TABLE `crm_user_yhq_result_copy1` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `uid` varchar(255) DEFAULT NULL,
      `hard` int(10) DEFAULT NULL COMMENT '所有的难度值',
      `createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `score` int(10) DEFAULT NULL COMMENT '得分',
      `endTime` varchar(255) NOT NULL COMMENT '答题结束时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=913 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
    
    -- ----------------------------
    -- Records of crm_user_yhq_result_copy1
    -- ----------------------------
    BEGIN;
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (647, '9ddf45ff-31ef-4779-9991-0542e77ef259', 21, '2019-05-05 15:28:44', 180, '2019-05-05 15:30:42');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (665, '9ddf45ff-31ef-4779-9991-0542e77ef259', 23, '2019-05-05 17:04:21', 190, '2019-05-05 17:05:25');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (682, '492ea3de-5147-4d15-9582-93361d6fb8e4', 23, '2019-05-05 17:46:55', 190, '2019-05-05 17:47:55');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (830, 'e53fae5b-f6e4-4327-8817-765fc15a4b38', 22, '2019-05-07 17:37:18', 170, '2019-05-07 17:40:27');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (862, '3a351f04-50da-4e6e-9417-b62b55ef0dcb', 20, '2019-05-08 12:12:19', 200, '2019-05-08 12:15:48');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (863, 'd672c51e-f11b-4485-b8b6-308d8e31414c', 23, '2019-05-08 12:14:01', 180, '2019-05-08 12:15:54');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (865, '3a351f04-50da-4e6e-9417-b62b55ef0dcb', 22, '2019-05-08 12:23:01', 200, '2019-05-08 12:25:44');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (868, '3a351f04-50da-4e6e-9417-b62b55ef0dcb', 22, '2019-05-08 12:28:44', 190, '2019-05-08 12:32:02');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (878, '9ddf45ff-31ef-4779-9991-0542e77ef259', 22, '2019-05-08 14:13:57', 200, '2019-05-08 14:16:54');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (880, '3a351f04-50da-4e6e-9417-b62b55ef0dcb', 22, '2019-05-08 14:20:07', 200, '2019-05-08 14:23:10');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (881, '16e7a06f-2168-45c4-ab66-83d9fa06c7c8', 59, '2019-05-08 14:21:47', 170, '2019-05-08 14:22:02');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (883, '9ddf45ff-31ef-4779-9991-0542e77ef259', 21, '2019-05-08 14:25:12', 190, '2019-05-08 14:27:41');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (907, '9ddf45ff-31ef-4779-9991-0542e77ef259', 22, '2019-05-08 15:28:00', 180, '2019-05-08 15:28:57');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (909, '9ddf45ff-31ef-4779-9991-0542e77ef259', 22, '2019-05-08 17:57:20', 200, '2019-05-08 17:58:03');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (911, '9ddf45ff-31ef-4779-9991-0542e77ef259', 22, '2019-05-08 18:28:10', 190, '2019-05-08 18:29:01');
    INSERT INTO `crm_user_yhq_result_copy1` VALUES (912, '9ddf45ff-31ef-4779-9991-0542e77ef259', 22, '2019-05-08 18:31:24', 200, '2019-05-08 18:34:35');
    COMMIT;
    
    SET FOREIGN_KEY_CHECKS = 1;
    crm_user_yhq_result_copy1

    需求:根据score从高到低排序,并给出排名

    1、使用方法一:

    set @rownum=0;   #定义变量
    SELECT @rownum:=@rownum+1 as rank,crm_user_yhq_result_copy1.* from crm_user_yhq_result_copy1 ORDER BY score DESC;  #@rownum:=@rownum+1 表示变量rownum加一并重新赋值给rownum

    查询集如下:

    2、使用方法二:

    SELECT @rownum:=@rownum+1 as rank,crm_user_yhq_result_copy1.* from crm_user_yhq_result_copy1,(SELECT @rownum:=0) as temp ORDER BY crm_user_yhq_result_copy1.score desc

    两者的查询集一致,很显然方法二比方法一更好用写,一句sql搞定不需要另外定义变量。

    ghghgjhgjhgh
  • 相关阅读:
    22. Generate Parentheses
    21. Merge Two Sorted Lists
    20. Valid Parentheses
    19. Remove Nth Node From End of List
    18. 4Sum
    JDK7新特性
    类Enum
    装饰设计模式
    模板设计模式
    反射
  • 原文地址:https://www.cnblogs.com/just-do/p/10841974.html
Copyright © 2020-2023  润新知