SQL job creation table

In this which I mainly used a visual database connectivity tools, Navicat MYSQL, the other is MySQL, in fact, the subject itself is not difficult, the main issue is to understand the logic of

T_user create a table, the table has id, name, age field, id primary key from the growth, add some data into it, and prepare some of the same Age;
1. Find the three oldest records in the table
2. by age groups, each group calculated the maximum age of the above mentioned id
3. find the table maximum age of the user's name

Sql`
`Navicat MySQL Data Transfer`

`Source Server         : mysql`
`Source Server Version : 50022`
`Source Host           : your ip 
`Source Database       : test`

`Target Server Type    : MYSQL`
`Target Server Version : 50022`
`File Encoding         : 65001`

`Date: 2020-03-24 22:59:12`
`*/`

`SET FOREIGN_KEY_CHECKS=0;
`-- Table structure for t_user`
`DROP TABLE IF EXISTS t_user;`
`CREATE TABLE t_user (`
  `id int(16) NOT NULL auto_increment,`
  `name varchar(32) character set utf8 NOT NULL,`
  `age int(16) NOT NULL,`
  `PRIMARY KEY  (id)`
`) ENGINE=InnoDB DEFAULT CHARSET=latin1;`
`-- Records of t_user`
INSERT INTO t_user VALUES ('1', 'aa', '12');`
`INSERT INTO t_user VALUES ('2', 'ab', '13');`
`INSERT INTO t_user VALUES ('3', 'ac', '34');`
`INSERT INTO t_user VALUES ('4', 'ad', '45');`
`INSERT INTO t_user VALUES ('5', 'ae', '45');`
`INSERT INTO t_user VALUES ('6', 'af', '12');`
`INSERT INTO t_user VALUES ('7', 'ag', '56');`
`INSERT INTO t_user VALUES ('8', 'ah', '67');`
`INSERT INTO t_user VALUES ('9', 'ai', '7');`
`INSERT INTO t_user VALUES ('10', 'aj', '45');`
`INSERT INTO t_user VALUES ('11', 'ak', '3');`
`INSERT INTO t_user VALUES ('12', 'al', '66');`
`INSERT INTO t_user VALUES ('13', 'am', '3');`
`INSERT INTO t_user VALUES ('14', 'an', '5');`
`INSERT INTO t_user VALUES ('15', 'ag', '8');`
`INSERT INTO t_user VALUES ('16', 'ao', '66');`
`INSERT INTO t_user VALUES ('17', 'az', '8');`

1. Find the three oldest records in the table

SELECT *   FROM  t_user  ORDER BY age DESC LIMIT 0,3;

2. grouped by age, the age of each group obtained the largest id

SELECT id   FROM t_user    GROUP BY  age ORDER BY id DESC LIMIT 0,1 ;

3. Find the table maximum age of the user's name

SELECT name FROM t_user ORDER BY age DESC LIMIT 0,1;

Published 32 original articles · won praise 9 · views 3157

Guess you like

Origin blog.csdn.net/weixin_43501566/article/details/105084877