table of Contents
@
This blog is translated from two blog:
- http://www.mysqltutorial.org/mysql-cross-join/
https://www.w3resource.com/mysql/advance-query-in-mysql/mysql-cross-join.php
1. cross join Introduction
MySQL cross join is a connection in mysql, different from the inner and outer joins, connection for cross join, in fact, is the use of Cartesian connection. In MySQL, when not using a WHERE clause CROSS JOIN, CROSS JOIN produces a result set, the result set is the product of two rows association table. Typically, if each table having n rows and m, the result set will have n * m rows
Https://www.w3resource.com/mysql/advance-query-in-mysql/mysql-cross-join.php reference picture, as a demonstration of the process of cross join, this process is actually a Cartesian join query
2. cross join usage
cross join Usage:
SELECT * FROM t1
CROSS JOIN t2;
Note: cross join is not required on time or using keywords, this is different from the inner join and join the
If adding a WHERE clause conditions in t1 and t2 have the relationship table, the work CROSS JOIN INNER JOIN clauses similar to the query shown in the following:
SELECT * FROM t1
CROSS JOIN t2
WHERE t1.id = t2.id;
ok, then click include cross join table as an example of the table is derived
SELECT *
FROM table111
LEFT JOIN(table112 CROSS JOIN table113)
ON table111.id=table113.id;
ok, it introduces the simple use of cross join now take http://www.mysqltutorial.org/mysql-cross-join/ example to introduce:
First, create a new database salesdb:
CREATE DATABASE IF NOT EXISTS salesdb;
Secondly, the current switching data to the new database testdb:
USE testdb;
Salesdb create new tables in the database:
- The product master data table contains products, including the product ID, product name and selling price.
- The table stores include stores sell products.
- The table includes sales of products sold by number and date in a particular store.
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100),
price DECIMAL(13,2 )
);
CREATE TABLE stores (
id INT PRIMARY KEY AUTO_INCREMENT,
store_name VARCHAR(100)
);
CREATE TABLE sales (
product_id INT,
store_id INT,
quantity DECIMAL(13 , 2 ) NOT NULL,
sales_date DATE NOT NULL,
PRIMARY KEY (product_id , store_id),
FOREIGN KEY (product_id)
REFERENCES products (id)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (store_id)
REFERENCES stores (id)
ON DELETE CASCADE ON UPDATE CASCADE
);
The data into three tables. Suppose we have three products iPhone, iPad and Macbook Pro Shops sold in two North and South.
INSERT INTO products(product_name, price)
VALUES('iPhone', 699),
('iPad',599),
('Macbook Pro',1299);
INSERT INTO stores(store_name)
VALUES('North'),
('South');
INSERT INTO sales(store_id,product_id,quantity,sales_date)
VALUES(1,1,20,'2017-01-02'),
(1,2,15,'2017-01-05'),
(1,3,25,'2017-01-05'),
(2,1,30,'2017-01-02'),
(2,2,35,'2017-01-05');
ok, business scenarios: now a total of statistics for each store for each commodity turnover is how much money?
Obviously, with SUM (quantity * price), then group by what you can, write this sql good
SELECT
sto.`store_name`,
pro.`product_name`,
SUM(quantity * price) AS revenue
FROM
sales sal
INNER JOIN stores sto
ON sto.`id` = sal.`store_id`
INNER JOIN products pro
ON sal.`product_id` = pro.`id`
GROUP BY sto.`store_name`,pro.`product_name`;
ok, looked and found not sell the merchandise is not figured out, so he did not meet the business needs of business is to count all the store merchandise, so you can connect with a cross join Descartes, come all the stores merchandise mix data
Descartes combined data query sql:
SELECT
a.`store_name`,
b.product_name
from stores cross join products
Statistical front sql already have, so the combination of SQL and SQL data statistics of the association:
SELECT
a.`store_name`,
b.product_name,
IFNULL(c.revenue, 0) AS revenue
FROM
stores a
CROSS JOIN products b
LEFT JOIN
(SELECT
sto.`id` AS store_id,
pro.`id` AS product_id,
sto.`store_name`,
pro.`product_name`,
SUM(quantity * price) AS revenue
FROM
sales sal
INNER JOIN stores sto
ON sto.`id` = sal.`store_id`
INNER JOIN products pro
ON sal.`product_id` = pro.`id`
GROUP BY sto.`store_name`,
pro.`product_name`) c
ON a.id = c.store_id
AND b.id = c.product_id
ORDER BY a.store_name ;
Please note, IFNULL if the income is NULL (in the case of the store did not sell), query using the function returns 0.
CROSS JOIN used in this way by this clause, you can answer a wide range of issues, for example, by salesperson, sales in January to find, even if the salesperson does not sell in a given month.
ok, this blog is a translation of two English blog:
- http://www.mysqltutorial.org/mysql-cross-join/
- https://www.w3resource.com/mysql/advance-query-in-mysql/mysql-cross-join.php
ok, this two blog content translated from English blog, but certain finishing this blog, will be understood in two blog content integrated into this Chinese blog, because these two examples of blog is good, cited cross join common usage scenarios, of course, in addition to the use of two blog raised, cross join because of the nature of its Cartesian connections, can also be used to batch write data, the corresponding batch wording, MySQL can refer to my previous blog, this blog belongs to the nature of translation , so please indicate the source