Home Database Mysql Tutorial About MySQL database A collection of add, delete, modify and query statements

About MySQL database A collection of add, delete, modify and query statements

Oct 13, 2017 am 10:27 AM
mysql database statement

1. Basic sql statements

CRUD操作:
create 创建(添加)
read 读取
update 修改
delete 删除
Copy after login

1. Add data

insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ; 
给特定的列添加数据
insert into Info (code,name) values('p010','李四');
自增长列的处理
insert into family values('','p001','数据','T001','数据',1);
insert into 表名 values(值)
Copy after login

2. Delete data

删除所有数据
delete from family
删除特定的数据
delete from Info where code='p001'
delete from 表名 where 条件
Copy after login

3. Modify data

修改所有数据
update Info set name='徐业鹏' 
修改特定数据
update Info set name='吕永乐' where code='p002' 
修改多列
update Info set name='吕永乐',sex=1 where code='p003' 
update 表名 set 要修改的内容 where 条件  tno =
Copy after login

4. Read data

(1)简单读取,查询所有列(*)  所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name,class from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5)关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符 
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询
select * from car order by powers  #默认升序排列
select * from car order by powers  #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc
Copy after login

(7) Range query

select * from car where price9()>40 and price<60
select * from car where price between 40 and 60
Copy after login

(8) Discrete query

select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)取出数据
select * from car where price not in(30,40,50,60)去掉数据
Copy after login

(9) Aggregation function (statistical query)

select count(*) from car
select count(code) from car #取所有的数据条数
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值
Copy after login

(10)Paging query

select * from car limit 0,10  #分页查询,跳过几条数据(0)取几条(10)
规定一个每页显示的条数:m
当前页数:n]
select * from car limit (n-1)*m,m
Copy after login

(11) Deduplication query

select distinct brand from car
Copy after login

(12) Group query
Query the number of cars in each series in the car table

select brand,count(*) from car group by brand
Copy after login

After grouping, only this column or aggregate function can be queried

Get the series code where the average price of the series is greater than 40

select brand from car group by brand having(加条件) avg(price)>40
Copy after login

Get the series code where the maximum fuel consumption of the series is greater than 8

select brand from car group by brand having max(oil)>8
Copy after login

2. Advanced query of MySql (Use external connection)

Connection query

SELECT t1.Name,t2.Brand_Name FROM brand t2,car t1 -- 笛卡尔乘积
WHERE t2.Brand = t1.Brand
Copy after login

--Multi-table join query

SELECT t1.Name,t2.Brand_Name,t3.prod_name  FROM car t1 LEFT JOIN brand t2 ON t1.Brand = t2.Brand
LEFT JOIN productor t3 ON t2.Prod = t3.Prod
Copy after login

--The number of fields in joint query must be the same

SELECT `Name`,Price FROM car 
UNION
 SELECT Brand_Name,Brand_Memo FROM brand
-- 子查询(***)
SELECT * FROM car WHERE car.brand in (SELECT Brand FROM brand WHERE Prod = &#39;p001&#39;)
Copy after login

Description: Use outer join

##A、left outerjoin

Left outer join (Left join): The result set includes matching rows of the join table and all rows of the left join table.

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

##B:right outerjoin:

Right outer join (right join): The result set includes both the matching join rows of the join table and all rows of the right join table.

##C:full/cross outerjoin

Full outer join: not only includes matching rows of the symbolic connection table, but also includes all records in the two connected tables.

##D:

Group:Group by:

<span style="font-size: 16px"><strong></strong></span>A table , once the grouping is completed, only group-related information can be obtained after querying.

##Group related information: (Statistical information) count,sum,max,min,avg Standards for grouping)

##When grouping in SQL Server: cannot be Fields of text, ntext, and image types are used as grouping basis

<p class="line number60 index59 alt1"><span style="font-size: 16px"><strong><code class="sql spaces"> The fields in the selecte statistical function cannot be placed together with ordinary fields;

E:Outer join query (table name 1: a table name 2: b)

##select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

F: The usage of between, between includes boundary values ​​when limiting the query data range, not betweenNot including

##select * from table1 where time between time1 and time2

##select a,b,c, from table1 where a not between Value 1 and Value 2

##G:

Four-table joint query problem: ##select

* from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ....H:

:First 10 records

##select

top

10 * form table1 where Range I:Select all the information of the record with the largest a in each group of data with the same b value (can be used for monthly forum rankings, monthly hot-selling product analysis, by subject Score ranking, etc.)

select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

The above is the detailed content of About MySQL database A collection of add, delete, modify and query statements. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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 Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

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

phpMyAdmin comprehensive use guide phpMyAdmin comprehensive use guide Apr 10, 2025 pm 10:42 PM

phpMyAdmin is not just a database management tool, it can give you a deep understanding of MySQL and improve programming skills. Core functions include CRUD and SQL query execution, and it is crucial to understand the principles of SQL statements. Advanced tips include exporting/importing data and permission management, requiring a deep security understanding. Potential issues include SQL injection, and the solution is parameterized queries and backups. Performance optimization involves SQL statement optimization and index usage. Best practices emphasize code specifications, security practices, and regular backups.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles