1. Introduction This blog will be very basic, if you have First, prepare a table with the following structure: SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `name` varchar(255) NOT NULL COMMENT 'User name', `age` int(11) NOT NULL COMMENT 'Age', `sex` smallint(6) NOT NULL COMMENT 'Gender', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; The table data is as follows: INSERT INTO `user` VALUES (1, '李子8', 18, 1); INSERT INTO `user` VALUES (2, '张三', 22, 1); INSERT INTO `user` VALUES (3, '李四', 38, 1); INSERT INTO `user` VALUES (4, '王五', 25, 1); INSERT INTO `user` VALUES (5, '六麻子', 13, 0); INSERT INTO `user` VALUES (6, '田七', 37, 1); SET FOREIGN_KEY_CHECKS = 1; Note that after 2. select2.1 Querying a Single Column First, use mysql> use liziba; Database changed Next, use select column_name from table_name; mysql> select name from user; +--------+ | name | +--------+ | Plum Eight| | Zhang San| | Li Si| | Wang Wu| | Liu Mazi| | Tianqi| +--------+ 6 rows in set (0.00 sec) 2.2 Querying multiple columnsThe difference between querying multiple columns and a single column is that select is followed by multiple column names, separated by commas. select column_name1,column_name2,column_name3 from table_name; mysql> select name,age from user; +--------+-----+ | name | age | +--------+-----+ | Plum Eight | 18 | | Zhang San | 22 | | Li Si | 38 | | Wang Wu| 25 | | Six pockmarks | 13 | | Tianqi | 37 | +--------+-----+ 6 rows in set (0.00 sec) 2.3 Query all columnsThere are two ways to query all columns. The first is the two derived methods above, listing all column names. mysql> select id,name,age,sex from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | +----+--------+-----+-----+ 6 rows in set (0.00 sec) The second type, which is also the most commonly used select * from table_name; mysql> select * from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | +----+--------+-----+-----+ 6 rows in set (0.00 sec)
3. distinct If you need to query data with unique column values, you can use We insert a new data into the above table. The data mysql> insert into user (name, age, sex) values('谢礼', 18, 1); Query OK, 1 row affected (0.01 sec) Now you can see that the age column has equal values mysql> select * from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | | 7 | Thank you | 18 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) At this point we want to get the ages of the users in the mysql> select distinct age from user; +-----+ |age| +-----+ | 18 | | 22 | | 38 | | 25 | | 13 | | 37 | +-----+ 6 rows in set (0.00 sec) There is one thing you need to note here . For example, if there is no unique data for both mysql> select distinct age,name from user; +-----+--------+ | age | name | +-----+--------+ | 18 | Plum Eight | | 22 | Zhang San| | 38 | Li Si| | 25 | Wang Wu| | 13 | Six pockmarks | | 37 | Tianqi | | 18 | Thank you gift | +-----+--------+ 7 rows in set (0.00 sec) If the field values following the mysql> select distinct age,sex from user; +-----+-----+ | age | sex | +-----+-----+ | 18 | 1 | | 22 | 1 | | 38 | 1 | | 25 | 1 | | 13 | 0 | | 37 | 1 | +-----+-----+ 6 rows in set (0.00 sec) 4. Limit The previous query will return all records that meet the conditions. If we only need a specified number of records, we can use The value of mysql> select * from user limit 0; Empty set (0.00 sec) mysql> select * from user limit 1; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | +----+--------+-----+-----+ 1 row in set (0.00 sec) If the value given by mysql> select count(1) from user; +----------+ | count(1) | +----------+ | 7 | +----------+ 1 row in set (0.01 sec) mysql> select * from user limit 8; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | | 7 | Thank you | 18 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec)
mysql> select * from user limit 2, 4; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | +----+--------+-----+-----+ 4 rows in set (0.00 sec) This is the end of this article about the use of MySQL select, distinct, and limit. For more information about the use of MySQL select, distinct, and limit, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Complete steps to quickly build a vue3.0 project
>>: Detailed explanation of CSS multiple three-column adaptive layout implementation
mysql returns Boolean type In the first case, ret...
CSS has two pseudo-classes that are not commonly ...
Docker Toolbox is a solution for installing Docke...
Table of contents Prune regularly Mirror Eviction...
Table of contents Overview Code Implementation Su...
This article example shares the specific code of ...
Table of contents 1. Download JDK (take jdk1.8.0 ...
MySQL executes SQL through the process of SQL par...
JavaScript clothing album switching effect (simil...
HTML is made up of tags and attributes, which are...
cursor A cursor is a method used to view or proce...
The following are all performed on my virtual mac...
A certain distance can be set between cells in a ...
Preface Some people have asked me some MySQL note...
Since its launch in 2009, flex has been supported...