做一个三张表的视图
麻烦做一个三张表的视图,只要有列关联就全部显示成行,是用full join? 求各位大大给个答案,三张表的列分别为
1、id system_id module_id role_id classes_id datascope_value
2、id code name tablename keyfield
3、id class_id code value
第一个表和第三个表里的classes_id 是第二章表里的id 就这么一个地方是关联的。。。。求代码
------解决方案-------------------- CREATE TABLE T_CSDN_ONE
(
id INT IDENTITY(1,1) PRIMARY KEY,
SYSTEM_ID INT,
MODULE_ID INT,
ROLE_ID INT,
CLASSES_ID INT,
DATASCOPE_VALUE VARCHAR(50)
)
CREATE TABLE T_CSDN_TWO
(
ID INT IDENTITY(1,1) PRIMARY KEY,
CODE VARCHAR(50),
NAME VARCHAR(50),
TABLENAME VARCHAR(50),
KEYFIELD VARCHAR(50)
)
CREATE TABLE T_CSDN_THREE
(
ID INT IDENTITY(1,1) PRIMARY KEY,
CLASS_ID INT,
CODE VARCHAR(50),
VALUE VARCHAR(50)
)
CREATE VIEW T_CSDN_VIEWONE
AS
SELECT A.* FROM T_CSDN_ONE A
FULL JOIN T_CSDN_TWO B ON A.CLASSES_ID=B.ID
FULL JOIN T_CSDN_THREE C ON C.CLASS_ID=A.CLASSES_ID
SELECT * FROM T_CSDN_VIEWONE
DROP TABLE T_CSDN_ONE
DROP TABLE T_CSDN_TWO
DROP TABLE T_CSDN_THREE
DROP VIEW T_CSDN_VIEWONE
------解决方案--------------------看错了,修正
CREATE VIEW [V_XXX]
AS
SELECT t1.*,t2.*,t3.*
FROM t2 AS LEFT OUTER JOIN
t1 ON t1.classes_id = t2.id LEFT OUTER JOIN
t3 ON t3.class_id = t2.id
------解决方案--------------------CREATE VIEW [V_XXX]
AS
SELECT t1.*,t2.*,t3.*
FROM t2 AS LEFT OUTER JOIN
t1 ON t1.classes_id = t2.id LEFT OUTER JOIN
t3 ON t3.class_id = t2.id
可以使用left join也可以使用full join,这个具体看你的需要了。