SQL query (multi-table queries)

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

aims:

 Multi-table queries to now, the master is not very deep, just write it again tonight. (Little emotion)

 Into multi-table queries: query the cross-connect, the connection query, the outer join queries, subqueries.

First Data Preparation:

​
# 分类表
CREATE TABLE category (
  cid VARCHAR(32) PRIMARY KEY ,
  cname VARCHAR(50)
);

#商品表
CREATE TABLE products(
  pid VARCHAR(32) PRIMARY KEY ,
  pname VARCHAR(50),
  price INT,
  flag VARCHAR(2), #是否上架标记为:1表示上架、0表示下架
  category_id VARCHAR(32),
  CONSTRAINT products_fk FOREIGN KEY (category_id) REFERENCES category (cid)
);

#分类
INSERT INTO category(cid,cname) VALUES('c001','家电');
INSERT INTO category(cid,cname) VALUES('c002','服饰');
INSERT INTO category(cid,cname) VALUES('c003','化妆品');

INSERT INTO category(cid,cname) VALUES('c004','奢侈品');
#商品
INSERT INTO products(pid, pname,price,flag,category_id) VALUES('p001','联想',5000,'1','c001');
INSERT INTO products(pid, pname,price,flag,category_id) VALUES('p002','海尔',3000,'1','c001');
INSERT INTO products(pid, pname,price,flag,category_id) VALUES('p003','雷神',5000,'1','c001');

INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p004','JACK JONES',800,'1','c002');
INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p005','真维斯',200,'1','c002');
INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p006','花花公子',440,'1','c002');
INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p007','劲霸',2000,'1','c002');

INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p008','香奈儿',800,'1','c003');
INSERT INTO products (pid, pname,price,flag,category_id) VALUES('p009','相宜本草',200,'1','c003');

​

Multi-table query:

Cross join query:

     Note: Cross-join queries, Cartesian product that will happen.

     Syntax: SELECT * from A, B;

     problem:

SELECT * FROM products,category;

Inner join query:

 En inquiry into left outer join, right outer join.

Implicit within the connector: `SELECT * from A, B WHERE condition;`
connecting the display: `SELECT * from the Join Inner A B ON condition;

Question: What has been the shelves of goods classified information?

Code:

#隐式内连接
SELECT * FROM products p,category c WHERE c.cid=p.category_id AND p.flag='1';
#内连接
SELECT * FROM products p INNER JOIN category c ON c.cid=p.category_id WHERE p.flag='1';

Outer join query:

Divided into left outer join outer join query and right outer join query

Left outer join: left the Join Outer
right outer joins: right Outer the Join

Q: Query the number of each classification of all commodities?

Code:

#左外连接
SELECT c.cname,COUNT(p.category_id) FROM category c LEFT OUTER JOIN products p ON c.cid=p.category_id GROUP BY c.cname;

#右外连接
SELECT c.cname,COUNT(p.category_id) FROM products p RIGHT OUTER JOIN category c ON c.cid=p.category_id GROUP BY c.cname;

Subquery:

A select statement as a result of another part of grammar select (query, the query results, tables, etc.).
grammar:

    select .... query field ... from ... query table .. where ...

Problem: the query "Cosmetics" category shelves Product Details

Code:


#隐式内连接
SELECT * FROM products p,category c WHERE c.cid=p.category_id AND cname='化妆品';

##作为查询条件
SELECT *FROM products p WHERE p.category_id=(SELECT cid FROM category WHERE cname='化妆品');

##作为另一张表
SELECT *FROM products p ,(SELECT cid FROM category WHERE cname='化妆品') cc WHERE p.category_id=cc.cid;

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/longyanchen/article/details/93257044