Home > Database > Mysql Tutorial > body text

Some commonly used MySQL query commands

PHPz
Release: 2023-04-17 17:23:40
Original
1914 people have browsed it

MySQL is a popular relational database management system that is widely used in web development and data storage. Queries are an important part of data retrieval and analysis using MySQL. The following are some commonly used MySQL query commands.

  1. SELECT
    SELECT is the most commonly used MySQL query command. It is used to retrieve rows of data from a specified table and optionally select specific columns to return.

Syntax:

SELECT column_name(s) FROM table_name;
Copy after login

Select specific columns to return through the SELECT statement. The following is an example:

SELECT name, age FROM employee;
Copy after login
  1. WHERE
    The WHERE command is used to filter data rows in the table that meet specified conditions.

Syntax:

SELECT column_name(s) FROM table_name WHERE condition;
Copy after login

The following is an example:

SELECT name, age FROM employee WHERE age > 30;
Copy after login
  1. ORDER BY
    The ORDER BY command is used to perform a query on the result set based on specified conditions. Sort, by default in ascending order.

Syntax:

SELECT column_name(s) FROM table_name ORDER BY column_name ASC|DESC;
Copy after login

The following is an example:

SELECT name, age FROM employee ORDER BY age DESC;
Copy after login
  1. GROUP BY
    The GROUP BY command is used to pair pairs based on one or more columns The result set is grouped to calculate the aggregate function. Aggregation functions are a set of built-in functions for summarizing data, such as COUNT, AVG, SUM, etc.

Syntax:

SELECT column_name(s) FROM table_name GROUP BY column_name;
Copy after login

The following is an example:

SELECT age, COUNT(*) FROM employee GROUP BY age;
Copy after login
  1. JOIN
    The JOIN command is used to join two or more tables The data is combined into a result set.

Syntax:

SELECT column_name(s) FROM table_name1 JOIN table_name2 ON condition;
Copy after login

The following is an example:

SELECT employee.name, department.name FROM employee JOIN department ON employee.department_id = department.id;
Copy after login
  1. DISTINCT
    The DISTINCT command is used to return the only different value in the result set.

Syntax:

SELECT DISTINCT column_name(s) FROM table_name;
Copy after login

The following is an example:

SELECT DISTINCT age FROM employee;
Copy after login
  1. LIMIT
    The LIMIT command is used to limit the number of rows returned by the query.

Syntax:

SELECT column_name(s) FROM table_name LIMIT number;
SELECT column_name(s) FROM table_name LIMIT offset, number;
Copy after login

The first syntax is used to return the first n rows, and the second syntax is used to skip the first x rows and return n rows.

The following is an example:

SELECT name, age FROM employee LIMIT 10;
SELECT name, age FROM employee LIMIT 20, 10;
Copy after login

The above are some commonly used MySQL query commands. MySQL also provides many other commands, such as INSERT, UPDATE, DELETE, etc., which can be used for data manipulation and management. If you want to learn more about MySQL query commands, please refer to the MySQL documentation or attend a training course on the Internet.

The above is the detailed content of Some commonly used MySQL query commands. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!