


MySQL database query basics, simple query, conditional query, sorting query results
1. SELECT statement SELECT COL1, COL2, ....COLn FROM TABLE1, TABLE2, ....TABLEn
[WHERE CONDITIONS] -- Query conditions
[GROUP BY GROUP_BY_LIST] -- Query result grouping
[HAVING CONDITIONS] -- Query conditions - statistical results as conditions
[ORDER BY ORDER_LIST[ASC|DESC] -- Query result sorting
2. Simple query
1. Query all rows and columns in the table
eg: Query all rows and columns in the player table
select user_qq,user_name,user_sex,user_birthday,user_mobile from users;
select * from users;
2. Query some columns of the table
eg: Query from the player table Player QQ and nickname
select user_qq,user_name from users;
3. Use of alias
eg: Query player QQ and nickname from the player table and display it as ' Player QQ' and 'player nickname'
select user_qq as 'player QQ',user_name as 'player nickname' from users;
select user_qq 'player QQ',user_name 'player nickname' from users;
4.DISTINCT keyword-eliminate duplicate rows in the result set
eg: Display QQ of players who participated in the game, and require players who participated in multiple games not to display QQ repeatedly
select distinct user_qq from scores;
5.LIMIT keyword-specify the display range of the data in the result set
eg: Display the 3rd to 5th data in the player table
select * from users limit 2,3;
select*from users limit 3 ---Only display the first three pieces of data
三.Conditional query
1. Ordinary condition query
Syntax: SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]
eg1: Query the player information with QQ number 12301
select * from users where user_qq =12301;
eg2: Query data with scores greater than 2500
select *from scores where score>2500;
<> --- --Not equal to >= -----Greater than or equal to <= -----Less than or equal to
eg3: Query the score information of the game number 1 and the score is greater than 4000 points
select * from scores where gno=1 and score>4000;
Logical operators: and-- and
or-- or
#eg4: Query the score information of game numbers 1 and 2select * from scores where gno=1 or gno=2;2. Fuzzy queryeg1: Query scores with scores between 2500 (inclusive) and 3000 (inclusive)select *from scores where score>=2500 and score<=3000;select * from scores where score between 2500 and 3000;eg2: Query score information where score is not between 2500 (inclusive) and 3000 (inclusive)select * from scores where score not between 2500 and 3000;eg3: Query players born from January 1, 1987 to July 31, 1992select * from users where user_birthday between '1987-01-01' and '1992-0731';
Wildcard: '_' One character Branch like 'L_' Any length Route_Code Like 'AMS-%' [] Within the specified range Airbusno Like 'AB0[1-5 ] ' [^^] Not in parentheses, Airbusno Like' Ab0 [^] ' Eg4: Query all the player information of all the surnames of the Sun
## Select * from users where user_name like 'Sun%';
eg5: Query the information of all players whose surname is not Sun
select * from users where user_name not like 'Sun%';
3. Query empty Value operator
eg: Query the information of players whose birthday is null
select * from users where use_birthday is null;
eg: Query the players whose birthday is not NULL Information
select * from users where user_birthday is not null;
Four pairs of query results sorting
1. Sort the specified column (sort by , sorting method)
Syntax: SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]
Example: Query all score information numbered 1 in the score table, and sort by score in ascending order
select *from scores where gno=1 order by score asc.
Example: Query all score information numbered 1 in the score table, and sort by score in descending order
select * from score where gno=1 order by score desc.
2. Sort multiple columns (sorting basis, sorting method, priority)
Example: Query all information in the score table , and sort in ascending order of game number and descending order of score
select * from scores order by gno asc, score desc
The above is the detailed content of MySQL database query basics, simple query, conditional query, sorting query results. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
