Home > Database > Mysql Tutorial > SQL statements that MySQl database must know

SQL statements that MySQl database must know

怪我咯
Release: 2017-04-30 10:27:55
Original
1141 people have browsed it

This article shares with you an article about the enhanced version of the MySQL database must know SQL statements. It is very good and has reference value. Friends who need it can refer to it.

This article belongs to the enhanced version. Questions and The sql statement is as follows.

Create users table, set id, name, gender, sal fields, where id is the primary key

drop table if exists users; 
create table if not exists users( 
  id int(5) primary key auto_increment, 
  name varchar(10) unique not null,   
  gender varchar(1) not null, 
  sal int(5) not null 
); 
insert into users(name,gender,sal) values('AA','男',1000); 
insert into users(name,gender,sal) values('BB','女',1200);
Copy after login

------- -------------------------------------------------- --------------------------

One-on-one: What is AA’s identity number

drop table if exists users; 
create table if not exists users( 
  id int(5) primary key auto_increment, 
  name varchar(10) unique not null,   
  gender varchar(1) not null, 
  sal int(5) not null 
); 
insert into users(name,gender,sal) values('AA','男',1000); 
insert into users(name,gender,sal) values('BB','女',1200); 
drop table if exists cards; 
create table if not exists cards( 
  id int(5) primary key auto_increment, 
  num int(3) not null unique, 
  loc varchar(10) not null, 
  uid int(5) not null unique, 
  constraint uid_fk foreign key(uid) references users(id) 
); 
insert into cards(num,loc,uid) values(111,'北京',1); 
insert into cards(num,loc,uid) values(222,'上海',2);
Copy after login

[Note: inner join means inner join]

select u.name "姓名",c.num "身份证号" 
from users u inner join cards c 
on u.id = c.uid 
where u.name = 'AA'; 
-- 
select u.name "姓名",c.num "身份证号" 
from users u inner join cards c 
on u.id = c.uid 
where name = 'AA';
Copy after login

----- ----------------------------------------

One-to-many: Query which employees are in the "Development Department"

Create groups table

drop table if exists groups; 
create table if not exists groups( 
  id int(5) primary key auto_increment, 
  name varchar(10) not null 
); 
insert into groups(name) values('开发部'); 
insert into groups(name) values('销售部');
Copy after login

Create emps table

drop table if exists emps; 
create table if not exists emps( 
  id int(5) primary key auto_increment, 
  name varchar(10) not null, 
  gid int(5) not null, 
  constraint gid_fk foreign key(gid) references groups(id) 
); 
insert into emps(name,gid) values('哈哈',1); 
insert into emps(name,gid) values('呵呵',1); 
insert into emps(name,gid) values('嘻嘻',2); 
insert into emps(name,gid) values('笨笨',2);
Copy after login

Check which employees are in the "Development Department"

select g.name "部门",e.name "员工" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = '开发部'; 
-- 
select g.name "部门",e.name "员工" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = '开发部';
Copy after login

--------- ---------------------------------------------

Many-to-many: Query which students "Zhao" has taught

Create students table

##

drop table if exists students; 
create table if not exists students( 
  id int(5) primary key auto_increment, 
  name varchar(10) not null 
); 
insert into students(name) values('哈哈'); 
insert into students(name) values('嘻嘻');
Copy after login

Create teachers table


drop table if exists teachers; 
create table if not exists teachers( 
  id int(5) primary key auto_increment, 
  name varchar(10) not null 
); 
insert into teachers(name) values('赵'); 
insert into teachers(name) values('刘');
Copy after login

Create the middles table primary key(sid,tid) represents the joint primary key, and the two fields must be unique as a whole


drop table if exists middles; 
create table if not exists middles( 
  sid int(5), 
  constraint sid_fk foreign key(sid) references students(id), 
  tid int(5), 
  constraint tid_fk foreign key(tid) references teachers(id), 
  primary key(sid,tid)  
); 
insert into middles(sid,tid) values(1,1); 
insert into middles(sid,tid) values(1,2); 
insert into middles(sid,tid) values(2,1); 
insert into middles(sid,tid) values(2,2);
Copy after login

Check which students "Zhao" has taught


select t.name "老师",s.name "学生" 
from students s inner join middles m inner join teachers t 
on (s.id=m.sid) and (m.tid=t.id) 
where t.name = '赵'; 
-- 
select t.name "老师",s.name "学生" 
from students s inner join middles m inner join teachers t  
on (s.id=m.sid) and (t.id=m.tid) 
where t.name = "赵";
Copy after login

------- -------------------------------------------------- --------------------------------------------------

Mark employees with a salary of more than 5,000 yuan (inclusive) as "high salary", otherwise mark them as "starting salary"


Mark employees with a salary of NULL as "no salary"


Mark employees who earn more than 5,000 yuan (inclusive) as "high salary", otherwise mark them as "starting salary"


Mark employees who earn 7,000 yuan as "high salary", and those who earn 6,000 yuan The employee is marked as "mid-salary", 5,000 yuan is marked as "starting salary", otherwise it is marked as "trial salary"

------------------- -------------------------------------------------- ------------------------------------

Inner join (equivalent join): Query customer name, order number, order price

[Note: customers c inner join orders o uses an alias, and o will represent orders in the future]


select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格" 
from customers c inner join orders o 
on c.id = o.customers_id; 
-- 
select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格" 
from customers c inner join orsers o 
on c.id = o.customers_id;
Copy after login

on+Conditions for connecting two tables. Primary key of one table, foreign key of one table

Inner join: Only records that exist in two tables according to the connection conditions can be queried, which is a bit Similar to intersection in mathematics

--------------------------------------------- -------------

Outer connection: Group by customer, query the name and order number of each customer

Outer connection: You can query based on the connection conditions Records that exist in both tables can also be queried based on one side, even if the other side's records are not satisfied with the conditions.

Outer joins can be subdivided into:


<左外连接 : 以左侧为参照,left outer join表示 
select c.name,count(o.isbn) 
from customers c left outer join orders o 
on c.id = o.customers_id 
group by c.name; 
-- 
>右外连接 : 以右侧为参照,right outer join表示 
select c.name,count(o.isbn) 
from orders o right outer join customers c 
on c.id = o.customers_id 
group by c.name;
Copy after login

left outer join means that the content on the left will be displayed. For example, customers c left out join means that all the contents of a certain column in customers will be found


------------------------------------------------ -----

Self-connection: Find out whether AA’s boss is EE. Think of yourself as two tables. One on each side

select users.ename,bosss.ename 
from emps users inner join emps bosss 
on users.mgr = bosss.empno; 
select users.ename,bosss.ename 
from emps users left outer join emps bosss 
on users.mgr = bosss.empno;
Copy after login

------------------------ -------------------------------------------------- ---------------------

Demonstrate functions in MySQL (query manual)

Date and time functions:


select addtime(&#39;2016-8-7 23:23:23&#39;,&#39;1:1:1&#39;);  时间相加 
select current_date(); 
select current_time(); 
select now(); 
select year( now() ); 
select month( now() ); 
select day( now() ); 
select datediff(&#39;2016-12-31&#39;,now());
Copy after login

String functions:

select charset(&#39;哈哈&#39;); 
select concat(&#39;你好&#39;,&#39;哈哈&#39;,&#39;吗&#39;); 
select instr(&#39;www.baidu.com&#39;,&#39;baidu&#39;); 
select substring(&#39;www.baidu.com&#39;,5,3);
Copy after login

Mathematical functions:

select bin(10); 
select floor(3.14);//比3.14小的最大整数---正3 
select floor(-3.14);//比-3.14小的最大整数---负4 
select ceiling(3.14);//比3.14大的最小整数---正4 
select ceiling(-3.14);//比-3.14大的最小整数---负3,一定是整数值 
select format(3.1415926,3);保留小数点后3位,四舍五入 
select mod(10,3);//取余数 
select rand();//
Copy after login


Encryption function:

select md5('123456');

Returns 32-bit hexadecimal System number e10adc3949ba59abbe56e057f20f883e


Demonstrates the process control statement in MySQL


use json; 
drop table if exists users; 
create table if not exists users( 
  id int(5) primary key auto_increment, 
  name varchar(10) not null unique, 
  sal int(5) 
); 
insert into users(name,sal) values(&#39;哈哈&#39;,3000); 
insert into users(name,sal) values(&#39;呵呵&#39;,4000); 
insert into users(name,sal) values(&#39;嘻嘻&#39;,5000); 
insert into users(name,sal) values(&#39;笨笨&#39;,6000); 
insert into users(name,sal) values(&#39;明明&#39;,7000); 
insert into users(name,sal) values(&#39;丝丝&#39;,8000); 
insert into users(name,sal) values(&#39;君君&#39;,9000); 
insert into users(name,sal) values(&#39;赵赵&#39;,10000); 
insert into users(name,sal) values(&#39;无名&#39;,NULL);
Copy after login

Identify employees with more than 5,000 yuan (inclusive) It is "high salary", otherwise it is marked as "starting salary"

select name "姓名",sal "薪水", 
    if(sal>=5000,"高薪","起薪") "描述" 
from users;
Copy after login


Identifies employees whose salary is NULL as "none" Salary"

select name "姓名",ifnull(sal,"无薪") "薪水" 
from users;
Copy after login


Mark employees who earn more than 5,000 yuan (inclusive) as "high salary", otherwise mark them as "minimum salary" Salary"

select name "姓名",sal "薪水", 
    case when sal>=5000 then "高薪" 
    else "起薪" end "描述" 
from users;
Copy after login

identifies employees earning 7,000 yuan as "high salary", employees earning 6,000 yuan as "medium salary", and employees earning 5,000 yuan as "starting salary" Salary", otherwise marked as "Trial Salary"

select name "姓名",sal "薪水", 
    case sal 
      when 3000 then "低薪" 
      when 4000 then "起薪" 
      when 5000 then "试用薪" 
      when 6000 then "中薪" 
      when 7000 then "较好薪" 
      when 8000 then "不错薪" 
      when 9000 then "高薪" 
      else "重薪" 
    end "描述" 
from users;
Copy after login

The above is the detailed content of SQL statements that MySQl database must know. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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