CREATE TABLE STORE
(
STORE_CODE INT,
STORE_NAME VARCHAR(20),
STORE_YTD_SALES NUMERIC,
REGION_CODE INT,
EMP_CODE INT
);
INSERT INTO STORE
VALUES ('1', 'Access Junction', '1003455.76', '2', '8'),
('2', 'Database Corner', '1421987.39', '2', '12'),
('3', 'Tuple Charge', '986783.22', '1', '7'),
('4', 'Attribute Alley', '944568.56', '2', '3'),
('5', 'Primary Key Point', '2930098.45', '1', '15');
CREATE TABLE REGION
(
REGION_CODE INT,
REGION_DESCRIPT VARCHAR(10)
);
INSERT INTO REGION
VALUES ('1', 'East'), ('2', 'West');
我是SQL新手我需要列出所有商店和地区,如下例所示:
Code Description
----------- --------------------
1 Access Junction
1 East
2 Database Corner
但我不确定如何使其发挥作用。请有人帮帮我吗?
答案 0 :(得分:0)
解决您的问题:
SELECT Code, Description
FROM (
SELECT STORE_CODE AS Code, Store_Name AS Description
FROM Store
UNION ALL
SELECT REGION_CODE AS Code, REGION_DESCRIPT AS Description
FROM REGION
) AS t
ORDER BY Code,Description
<强>输出:强>
Code Description
---------------------------
1 Access Junction
1 East
2 Database Corner
2 West
3 Tuple Charge
4 Attribute Alley
5 Primary Key Point
演示链接: