sql
-- 创建事实表
CREATE TABLE product_facts (
id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255),
price DECIMAL(10, 2)
);
-- 插入数据
INSERT INTO product_facts (product_name, price) VALUES ('Product A', 100.00);
INSERT INTO product_facts (product_name, price) VALUES ('Product B', 150.50);
INSERT INTO product_facts (product_name, price) VALUES ('Product C', 200.25);
INSERT INTO product_facts (product_name, price) VALUES ('Product D', 75.80);
INSERT INTO product_facts (product_name, price) VALUES ('Product E', 300.00);
-- 创建产品维度表
CREATE TABLE product_dimension (
product_name VARCHAR(255) PRIMARY KEY,
product_attribute VARCHAR(255),
product_source VARCHAR(255)
);
-- 插入数据
INSERT INTO product_dimension (product_name, product_attribute, product_source)
VALUES ('Product A', 'Attribute 1', 'Source 1');
INSERT INTO product_dimension (product_name, product_attribute, product_source)
VALUES ('Product B', 'Attribute 2', 'Source 2');
INSERT INTO product_dimension (product_name, product_attribute, product_source)
VALUES ('Product C', 'Attribute 3', 'Source 3');
INSERT INTO product_dimension (product_name, product_attribute, product_source)
VALUES ('Product F', 'Attribute 6', 'Source 6');
-
product_facts
-
product_dimension
-
inner join
sql
SELECT * from product_facts t1 inner join product_dimension t2 on t1.product_name=t2.product_name;
- left join
sql
SELECT * from product_facts t1 left join product_dimension t2 on t1.product_name=t2.product_name;
- right join
sql
SELECT * from product_facts t1 RIGHT join product_dimension t2 on t1.product_name=t2.product_name;
- full join
sql
SELECT * from product_facts t1 FULL JOIN product_dimension t2 on t1.product_name=t2.product_name;
--在MySQL中,没有FULL JOIN关键字,而是使用LEFT JOIN和RIGHT JOIN的组合来模拟FULL JOIN
SELECT *
FROM product_facts t1
LEFT JOIN product_dimension t2 ON t1.product_name = t2.product_name
UNION
SELECT *
FROM product_facts t1
RIGHT JOIN product_dimension t2 ON t1.product_name = t2.product_name;
- 使用LEFT SEMI JOIN时,通常是为了获取左表中存在于右表的数据,但不需要右表的数据
sql
--MySQL
SELECT
t1.*
FROM
product_facts t1
LEFT JOIN product_dimension t2 ON t1.product_name = t2.product_name
WHERE
t2.product_name IS NOT NULL;
-- 子查询中的EXISTS子句会检查表B中是否存在与表A中的行相匹配的数据。如果存在匹配的数据,那么这些行就会被包含在结果集中。
SELECT *
FROM product_facts t1
WHERE EXISTS (
SELECT 1
FROM product_dimension t2
WHERE t1.product_name = t2.product_name
);
-- hive sql
-- 这个查询使用LEFT SEMI JOIN从表A中选择那些在表B中存在匹配的行的数据。LEFT SEMI JOIN只返回左表中存在于右表的行,而不返回右表的数据。
SELECT A.*
FROM table_a A
LEFT SEMI JOIN table_b B ON A.column = B.column;