当前位置:网站首页>MySQL of database -- basic common query commands
MySQL of database -- basic common query commands
2022-04-23 09:20:00 【King_ nul】
Catalog
MySQL Common basic commands of database
One 、 Basic query command syntax
Two 、 Give the result set of the query names
3、 ... and 、 De duplication query operation
Four 、MySQL in “+” The sign operator
MySQL Common basic commands of database
Foregoing : Study , We need to lay a solid foundation , Only with a solid foundation can we proceed step by step , Other commands after learning well .( Come on! !! Learning army )
One 、 Basic query command syntax
SELECT Query list FROM Table name
characteristic :
1、 The query list can be Fields in the table 、 Constant values 、 expression 、 function
2、 The query result set is a virtual table ( A table that does not exist in the database )
practice :
# First create a new database
CREATE DATABASE IF NOT EXISTS test;
# Enter the created database
USE test;
# Create new table
CREATE TABLE IF NOT EXISTS test_tb(id int(5),name varchar(10));
# Insert several pieces of data ( The insert data command will be discussed later )
INSERT INTO test_tb VALUES(1,"K"),(2,"J"),(3,"L");

# stay MySQL in Keywords are normalized in uppercase
# The query syntax
#SELECT Query list FROM Table name
# Query a single field
mysql> SELECT id FROM test_tb;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
4 rows in set (0.04 sec)
# Query multiple fields
mysql> SELECT id,name FROM test_tb;
+----+------+
| id | name |
+----+------+
| 1 | K |
| 2 | J |
| 3 | L |
| 4 | h |
+----+------+
4 rows in set (0.03 sec)
# Query all fields
# Mode one
mysql> SELECT * FROM test_tb;
+----+------+
| id | name |
+----+------+
| 1 | K |
| 2 | J |
| 3 | L |
| 4 | h |
+----+------+
4 rows in set (0.03 sec)
# Mode two
mysql> SELECT id,name FROM test_tb;
+----+------+
| id | name |
+----+------+
| 1 | K |
| 2 | J |
| 3 | L |
| 4 | h |
+----+------+
4 rows in set (0.03 sec)
# Query constant value
mysql> SELECT 100;
+-----+
| 100 |
+-----+
| 100 |
+-----+
1 row in set (0.03 sec)
mysql> SELECT "string";
+--------+
| string |
+--------+
| string |
+--------+
1 row in set (0.03 sec)
# Query expression
mysql> SELECT 100+2;
+-------+
| 100+2 |
+-------+
| 102 |
+-------+
1 row in set (0.03 sec)
mysql> SELECT 100-2;
+-------+
| 100-2 |
+-------+
| 98 |
+-------+
1 row in set (0.03 sec)
mysql> SELECT 100*2;
+-------+
| 100*2 |
+-------+
| 200 |
+-------+
1 row in set (0.03 sec)
mysql> SELECT 100/2;
+---------+
| 100/2 |
+---------+
| 50.0000 |
+---------+
1 row in set (0.03 sec)
# Query function
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.33 |
+-----------+
1 row in set (0.03 sec)
Case study : Inquire about test_tb All of the id
SELECT id FROM test_tb;
Two 、 Give the result set of the query names
Alias : seeing the name of a thing one thinks of its function , It's another name , Because the result set of the query is a virtual table , Therefore, there is no name, only the field name of the queried table , So for the convenience of viewing and understanding , We should learn to alias our watches .
characteristic
① Easy to understand
② If the fields to be queried have duplicate names , Use aliases to distinguish
# names :
# Mode one : Use AS
SELECT 100*2 AS result ;
mysql> SELECT 100*2 AS result ;
+------+
| result |
+------+
| 200 |
+------+
1 row in set (0.04 sec)
SELECT id AS id Number FROM test_tb;
mysql> SELECT id AS id Number FROM test_tb;
+------+
| id Number |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.03 sec)
# Mode two : Use spaces
SELECT 100*2 result ;
mysql> SELECT 100*2 result ;
+------+
| result |
+------+
| 200 |
+------+
1 row in set (0.04 sec)
SELECT id id Number FROM test_tb;
mysql> SELECT id id Number FROM test_tb;
+------+
| id Number |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.04 sec)
# Case study : Inquire about test_tb surface name Field Alias as out put
mysql> mysql> SELECT name out put FROM test_tb;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> SELECT name out put FROM test_tb' at line 1
# Use error reporting directly
mysql> SELECT name "out put" FROM test_tb;
+---------+
| out put |
+---------+
| K |
| J |
| L |
| h |
+---------+
4 rows in set (0.03 sec)
# Because there are spaces in the alias ,MySQL Not directly identifiable , So you need to enclose those with spaces in double quotation marks .
3、 ... and 、 De duplication query operation
The de duplication operation is set to prevent unnecessary repetition .
# First, add a few rows to the table we want to query id Same data
mysql> INSERT INTO test_tb VALUES(1,"A"),(4,"q"),(4,"w");
Query OK, 3 rows affected (0.13 sec)
Records: 3 Duplicates: 0 Warnings: 0
# Data added successfully
# First query all the data in the table
mysql> SELECT * FROM test_tb;
+----+------+
| id | name |
+----+------+
| 1 | K |
| 2 | J |
| 3 | L |
| 4 | h |
| 1 | A |
| 4 | q |
| 4 | w |
+----+------+
7 rows in set (0.03 sec)
# There are seven pieces of data
mysql> SELECT id FROM test_tb;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 1 |
| 4 |
| 4 |
+----+
7 rows in set (0.03 sec)
# Go to check again (DISTINCT)
mysql> SELECT DISTINCT id FROM test_tb;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
4 rows in set (0.03 sec)
Four 、MySQL in “+” The sign operator
#mysql Medium + Number :
# There's only one function : Is to be an operator
# Both query values are numeric , Then add
mysql> SELECT 100+90;
+--------+
| 100+90 |
+--------+
| 190 |
+--------+
1 row in set (0.03 sec)
# As long as one of them is character type , Trying to convert a character value to a numeric value , If the conversion is successful , Then continue to do the addition
mysql> select '100'+90;
+----------+
| '100'+90 |
+----------+
| 190 |
+----------+
1 row in set (0.03 sec)
# If the conversion fails , Then convert the character value to 0
mysql> SELECT 'k'+100;
+---------+
| 'k'+100 |
+---------+
| 100 |
+---------+
1 row in set (0.03 sec)
# As long as one of them is null, The result must be null
mysql> SELECT null+100;
+----------+
| null+100 |
+----------+
| NULL |
+----------+
1 row in set (0.03 sec)
#MySQL The join operation in is CONCAT
# Case inquiry id,name And connect the junction ;
mysql> SELECT CONCAT(id,name) AS Link FROM test_tb;
+------+
| Link |
+------+
| 1K |
| 2J |
| 3L |
| 4h |
| 1A |
| 4q |
| 4w |
+------+
7 rows in set (0.04 sec)
Finally, the used in the experiment is attached sql data , If you want to practice , You can take it directly
link : https://pan.baidu.com/s/1YkJMvhI17RE0pln8wQx5Ag Extraction code : evfg
These are the basic commands and understandings commonly used in my study , Beginners are welcome to learn and discuss .
版权声明
本文为[King_ nul]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230629527643.html
边栏推荐
- Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
- How to read excel table to database
- Trc20 fund collection solution based on thinkphp5 version
- Employee probation application (Luzhou Laojiao)
- Leetcode-199 - right view of binary tree
- Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- Find the sum of simple types of matrices
- Brief steps to build a website / application using flash and H5
- Non duplicate data values of two MySQL query tables
- What is monitoring intelligent playback and how to use intelligent playback to query video recording
猜你喜欢

《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路

The most concerned occupations after 00: civil servants ranked second. What was the first?

Non duplicate data values of two MySQL query tables

Multi view depth estimation by fusing single view depth probability with multi view geometry

ATSS(CVPR2020)

Chapter VIII project stakeholder management of information system project manager summary

Kettle实验 (三)

Kettle实验 (三)

小程序报错:Cannot read property 'currentTarget' of undefined

kettle实验
随机推荐
MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
Machine learning (VI) -- Bayesian classifier
112. 路径总和
考研线性代数常见概念、问题总结
SAP 101K 411K 库存变化
Kettle experiment (III)
Open services in the bottom bar of idea
Single sign on SSO
Applet error: cannot read property'currenttarget'of undefined
Emuelec compilation summary
Number of islands
[58] length of the last word [leetcode]
tsdf +mvs
Go language self-study series | golang method
108. Convert an ordered array into a binary search tree
Go language learning notes - slice, map | go language from scratch
Trc20 fund collection solution based on thinkphp5 version
Detailed explanation of delete, truncate and drop principles in MySQL database
Node installation
Four pictures to understand some basic usage of Matplotlib