Mysql database (6): detailed, very friendly to novices

Create a new database

Insert picture description here

  • Create a table of suppliers and insert data
    Insert picture description here
CREATE TABLE suppliers
(
 s_id   int   NOT NULL AUTO_INCREMENT,
 s_name  char(50) NOT NULL,
 s_city  char(50) NULL,
 s_zip   char(10) NULL,
 s_call  CHAR(50) NOT NULL,
 PRIMARY KEY (s_id)
) ;

In order to demonstrate the needs, we need to insert data

INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)
VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
(102,'LT Supplies','Chongqing','400000','44333'),
(103,'ACME','Shanghai','200000','90046'),
(104,'FNK Inc.','Zhongshan','528437','11111'),
(105,'Good Set','Taiyuang','030000', '22222'),
(106,'Just Eat Ours','Beijing','010', '45678'),
(107,'DK Inc.','Zhengzhou','450000', '33332');

Insert picture description here

  • Before querying, take a look at the structure of the two tables
    Insert picture description here
    Insert picture description here

1. Internal connection

  • Use inner join query between fruits table and suppliers table
SELECT suppliers.s_id, s_name,f_name, f_price FROM fruits ,suppliers WHERE fruits.s_id = suppliers.s_id;

Insert picture description here

  • Use inner join syntax for inner join query
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits
INNER JOIN suppliers ON fruits.s_id = suppliers.s_id;


Insert picture description here

Create another table orders

CREATE TABLE orders
(
 o_num int   NOT NULL AUTO_INCREMENT,
 o_date datetime NOT NULL,
 c_id  int   NOT NULL,
 PRIMARY KEY (o_num)
) ;

Insert picture description here
Insert data into the table
Insert picture description here

2. Left connection

SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;


Insert picture description here

3. Right connection

SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;


Insert picture description here

4. Compound condition join query

SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;


Insert picture description here

  • Use inner join query between fruits table and suppliers table. Before querying, check the structure of the two tables
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;
  • Use INNER JOIN syntax for inner join query between fruits table and suppliers table
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id;
  • Query other fruit types provided by fruit suppliers with f_id='a1'
SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';
  • In the customers table and orders table, query all customers, including customers without orders, the SQL syntax is as follows
SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;
  • In the customers table and orders table, query all orders, including orders without customers
SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;
  • In the customers table and orders table, use the INNER JOIN syntax to query the order information of the customer with ID 10001 in the customers table
SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;
  • Use INNER JOIN syntax between the fruits table and the suppliers table for inner join query and sort the query results
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

Continue to the next part ------------

Review the first five articles:

Mysql database (1): detailed, very friendly to novices

Mysql database (two): detailed, very friendly to novices

Mysql database (three): detailed, very friendly to novices

Mysql database (four): detailed, very friendly to novices

Mysql database (5): detailed, very friendly to novices

Guess you like

Origin blog.csdn.net/qq_43078445/article/details/108352419