sql classic example

1. The value of the series a plurality of columns

SELECT CONCAT(e.id,'=====',e.Salary) as 'case'
FROM employee e

2. Use conditional logic in the select statement in

3. Random returns several rows

select *
from employee
order by random() limit 3

4. The null value is converted to an actual value (case when it may be, but still not as good this function)

select coalesec(comm,0)
from emp

5. Find a match like (% _ ...)

6. sorted according to string

-- 依据ename字段,最后两个字母进行排序
select *
from emp
order by substr(ename,length(ename)-2)

7. When the sort processing of the null value (with a null query result value, a null value but want the top surface / back)

Use case when

8. A conditional logic to dynamically adjust sorting items

select *
from emp e
order by case when e.job='saleman' then comm else sal end

9. The superposition of two line sets

-- 注意这里union all的使用,union all是将一个结果集叠加到别人之上
select  e.id,departmentId,e.salary
from employee1 e
union all 
SELECT '-------------------',u.Role,null
FROM users u
union all
SELECT d.`name`,d.id,null
FROM department d

10. Insert Default

You can use default at the time to create a table to specify a default value for each field.

You can use the insert statement in use

11. Copy Table

insert into emp (ename,deptid,address)
select ename,deptid,address
from famliy;

12. Copy Table Structure

create table dept2
as
select * 
from dept1
where 1=0;

13. Multi-table joins inserted

mysql not holding support

14. IHP particular column

Create a view by providing a specific view

Update record 15. When there is a correlation line

update emp
set sal=sal*1.2-
where empno in (
select empno from emp_bouns)

16. Use another table data update record

update emp e 
set (e.sal,e.comm)=
(select ns.sal,ns.sal/2 
from new_sal ns
where ns.deptno=e.deptno)
where exists (
select null
from new_sal ns
where ns.deptno=e.deptno
)

17. Delete Record referential integrity violations

delete from emp
where not exixts (
    select * from dept where dept.deptno=emp.deptno
)

18. Remove duplicate rows

delete from dupes
where id not in (select min(d) 
from dupes group by name)

19. The records are deleted reference to other tables

delete from emp
where deptno in (
select deptno
from dept_accidents
group by deptno
having count(*) >=3
)

20. The metadata query

information_schema database is a database that contains all the tables

21. exemplified index column

show index

22. cited constraints

information_schema.table_constraints

23. The use sql sql generated (this is the key)

select 'select count(*) from '||table_name||';' cnts
from user_tables;

24. string processing (traversal strings, important skills)

select SUBSTR(e.name,e2.pos,1) as C
from (SELECT name from employee1 where name='Randy') e,(select id as pos from employee1 WHERE id<=5) e2
where e2.pos<=LENGTH(e.name)

25. Analyzing the character string containing alphanumeric

Regular Expressions

26. Write a note of here

select '192.168.2.1' as ip from rank
-- 这里会用相同的列去替换掉行

27. The difference between the calculated cumulative

 

 

Guess you like

Origin blog.csdn.net/qq_29164145/article/details/90168923