MySQL database DML related operations (data addition, deletion, modification, query related sql)

DML:data manipulation language

General CRUD

1. Data increase (insert)

insert into t_user(name,address) values('jwe','china');

2. Data deletion (delete)

delete from t_user where id=5;

3. Data modification (update)

update t_user set name='xixi' where id=1;

4. Data query (select)

  • General query
select  name,address from t_user;
  • Deduplication query
select distinct age from t_user;
  • Fuzzy query
select * from t_user where name like '%to%';
  • Aggregate query
select max(age) from t_user;
  • Group query
select count(*),age from t_user group by age having count(*)>1;
  • Sort query
select * from t_user order by age asc/desc;
  • Paging query (physical paging)
# limit 2、4指索引(第3到第5个),左闭右闭
select * from t_user limit 2,4;

Two: multi-table query

The essence of query: query XX target from XX range according to XX conditions

1. Subquery

  • where type [query condition]
select sno,cno from score where degree=(select max(degree) from score);
  • from type [query range]
  • exist type

2. Connect query

  • Foundation: Cartesian product t1*t2 【Scope of inquiry】
  • Equivalent connection
from  t1, t2 where  t1.deptid = t2.id
  • Internal connection
from  t1 inner join t2 on t1.x = t2.x
  • Left join [Table 1 is the main table]
from t1 left join t2 on t1.x = t2.x
  • Right connection [Table 2 is the main table]
from t1 right join t2 on t1.x=t2.x

3. Joint query

  • Application scenario:
    The results to be queried come from multiple tables, and multiple tables have no direct connection relationship, but the query information is consistent.

  • Condition: The results of multiple query statements can be combined into one table (the number of fields and types are the same).

  • Union query: Combine the results of multiple query statements into one result (not including duplicate data)

SELECT * FROM employees  WHERE email LIKE '%a%'
UNION
SELECT * FROM employees  WHERE department_id>90;
  • Union all query: Combine the results of multiple query statements into one result (including duplicate data)
SELECT id,cname FROM t_ca WHERE csex='男'
UNION ALL
SELECT t_id,tname FROM t_ua WHERE tGender='male';

4. Single table self-connection

select * from t_user a,t_user b where a.deptid = b.id

Guess you like

Origin blog.csdn.net/jw2268136570/article/details/103798597