MySQL database must know sql statements (enhanced version)

MySQL database must know sql statements (enhanced version)

This is an enhanced version. The questions and SQL statements are as follows.

Create a users table and set the id, name, gender, and sal fields, with id being 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);

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

One-to-one: What is AA's ID 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 cards exists; 
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);

[Note: inner join means inner join]

select u.name "name",c.num "ID number" 
from users u inner join cards c 
on u.id = c.uid 
where u.name = 'AA'; 
-- 
select u.name "name",c.num "ID number" 
from users u inner join cards c 
on u.id = c.uid 
where name = 'AA';

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

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

Create the 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('Development Department'); 
insert into groups(name) values('Sales Department');

Create the 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);

Check which employees are in the Development Department

select g.name "department",e.name "employee" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = 'Development Department'; 
-- 
select g.name "department",e.name "employee" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = 'Development Department';

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

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

Create the 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('嘻嘻');

Create the 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('刘');

Create the middles table. Primary key (sid, tid) represents the joint primary key. The two fields must be unique.

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);

Query which students "Zhao" has taught

select t.name "teacher",s.name "student" 
from students s inner join middles m inner join teachers t 
on (s.id=m.sid) and (m.tid=t.id) 
where t.name = 'Zhao'; 
-- 
select t.name "teacher",s.name "student" 
from students s inner join middles m inner join teachers t  
on (s.id=m.sid) and (t.id=m.tid) 
where t.name = "Zhao";

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

Employees with a salary of RMB 5,000 or above are marked as "high salary", otherwise they are marked as "starting salary"

Identify employees with a salary of NULL as "unpaid"

Employees with a salary of RMB 5,000 or above are marked as "high salary", otherwise they are marked as "starting salary"

Employees with a salary of 7,000 yuan are marked as "high salary", employees with a salary of 6,000 yuan are marked as "middle salary", and employees with a salary of 5,000 yuan are marked as "starting salary", otherwise they are 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, so o will represent orders from now on]

select c.name "Customer Name", o.isbn "Order Number", o.price "Order Price" 
from customers c inner join orders o 
on c.id = o.customers_id; 
-- 
select c.name "Customer Name", o.isbn "Order Number", o.price "Order Price" 
from customers c inner join orsers o 
on c.id = o.customers_id;

on+ The condition for connecting two tables. The primary key of one table and the foreign key of another table

Inner join: only records that exist in both tables according to the join conditions can be queried, which is somewhat similar to the intersection in mathematics.

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

Outer connection: Group by customer and query each customer's name and number of orders

Outer join: You can query the records in both tables based on the join conditions, or you can query the records in one table even if the other table does not meet the conditions.

Outer joins can be broken down into:

<Left outer join: With the left side as reference, left outer join means 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: With the right side as reference, right outer join means select c.name, count(o.isbn) 
from orders o right outer join customers c 
on c.id = o.customers_id 
group by c.name;

Left outer join means that all the contents on the left will be displayed, for example, customers c left out join means that all the contents of a column in customers will be found

------------------------------------------------------
Self-join: Find out that 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 bosses 
on users.mgr = bosss.empno; 
select users.ename,bosss.ename 
from emps users left outer join emps bosses 
on users.mgr = bosss.empno;

-----------------------------------------------------------------------------------------------
Demonstrating functions in MySQL (query manual)

Date and time functions:

select addtime('2016-8-7 23:23:23','1:1:1'); time addition select current_date(); 
select current_time(); 
select now(); 
select year( now() ); 
select month( now() ); 
select day( now() ); 
select datediff('2016-12-31',now());

String functions:

select charset('哈哈'); 
select concat('hello','haha','ma'); 
select instr('www.baidu.com','baidu'); 
select substring('www.baidu.com',5,3);

Mathematical functions:

select bin(10); 
select floor(3.14); //The largest integer smaller than 3.14 --- positive 3 
select floor(-3.14); //The largest integer smaller than -3.14 --- negative 4 
select ceiling(3.14); //The smallest integer greater than 3.14 --- positive 4 
select ceiling(-3.14);//The smallest integer greater than -3.14 --- negative 3, must be an integer value select format(3.1415926,3); keep 3 decimal places, round up select mod(10,3);//get the remainder select rand();//

Encryption function:

select md5('123456');

Returns the 32-bit hexadecimal number e10adc3949ba59abbe56e057f20f883e

Demonstrate flow control statements 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('哈哈',3000); 
insert into users(name,sal) values('呵',4000); 
insert into users(name,sal) values('嘻嘻',5000); 
insert into users(name,sal) values('笨笨',6000); 
insert into users(name,sal) values('明明',7000); 
insert into users(name,sal) values('丝丝',8000); 
insert into users(name,sal) values('君君',9000); 
insert into users(name,sal) values('赵赵',10000); 
insert into users(name,sal) values('无名',NULL);

Employees with a salary of RMB 5,000 or above are marked as "high salary", otherwise they are marked as "starting salary"

select name "name",sal "salary", 
    if(sal>=5000,"high salary","starting salary") "description" 
from users;

Identify employees with a salary of NULL as "unpaid"

select name "name",ifnull(sal,"unpaid") "salary" 
from users;

Employees with a salary of RMB 5,000 or above are marked as "high salary", otherwise they are marked as "starting salary"

select name "name",sal "salary", 
    case when sal>=5000 then "high salary" 
    else "Starting salary" end "Description" 
from users;

Employees with a salary of 7,000 yuan are marked as "high salary", employees with a salary of 6,000 yuan are marked as "middle salary", and employees with a salary of 5,000 yuan are marked as "starting salary", otherwise they are marked as "trial salary"

select name "name",sal "salary", 
    case sal 
      when 3000 then "low salary" 
      when 4000 then "starting salary" 
      when 5000 then "trial salary" 
      when 6000 then "middle salary" 
      when 7000 then "better salary" 
      when 8000 then "not bad salary" 
      when 9000 then "high salary" 
      else "heavy salary" 
    end "Description" 
from users;

The above is the must-know SQL statements for MySQl database (enhanced version) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to use C to execute SQL statements in MySQL database
  • Summary of six useful SQL statements for MySQL database operations
  • MySql database alter table SQL statement set
  • Summary of basic SQL statements in MySQL database
  • MySQL 5.7.14 download and installation graphic tutorial and MySQL database statement entry
  • Detailed explanation of MySQL database insert and update statements
  • Detailed explanation of optimizing query statements in MySQL database
  • MySQL database rename statement sharing
  • Detailed explanation of the usage of INSERT, UPDATE, DELETE, and REPLACE statements in MySQL database
  • Advanced and summary of commonly used sql statements in MySQL database

<<:  How to fix the WeChat applet input jitter problem

>>:  Differences between this keyword in NodeJS and browsers

Recommend

Vue3 (Part 2) Integrating Ant Design Vue

Table of contents 1. Integrate Ant Design Vue 2. ...

Automatically build and deploy using Docker+Jenkins

This article introduces Docker+Jenkins automatic ...

A comprehensive analysis of what Nginx can do

Preface This article only focuses on what Nginx c...

HTML table markup tutorial (1): Creating a table

<br />This is a series of tutorials provided...

How to set static IP in centOS7 NET mode

Preface NAT forwarding: Simply put, NAT is the us...

Solutions to Mysql index performance optimization problems

The optimization created by MySQL is to add index...

How to install MySQL 5.7.29 with one click using shell script

This article refers to the work of 51CTO blog aut...

How to get the current time using time(NULL) function and localtime() in Linux

time(); function Function prototype: time_t time(...

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environmen...

Implementation of tomcat image created with dockerfile based on alpine

1. Download the alpine image [root@docker43 ~]# d...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...