简体   繁体   English

如何使用4个表创建视图?

[英]How to create a View with 4 tables?

I have four tables 我有四张桌子

Table 1
-------------
primary key-pk1


Table 2
------------
primary key-pk2
foreign key-pk1


Table 3
------------
primary key-pk3
foreign key-UID


Table 4
------------
primary key-pk4
foreign keys-pk1,UID,pk3


Table U
------------
primary key-UID

I wanted to create a View with tables Table 1, Table 2, Table 3 and Table 4 in such a way that even if there is no contents in Table 2 and Table 3, I should get join ,ie matching contents, of Table 1 and Table 4. 我想用表格1,表2,表3和表4创建一个视图,即使表2和表3中没有内容,我也应该得到表1和表1的匹配内容。表4。

There will always be contents in Table 1 and Table 4. Table 2 and Table 3 may or may not have contents 表1和表4中总是有内容。表2和表3可能有也可能没有内容

Thanks in advance :) 提前致谢 :)

Try using left join. 尝试使用左连接。 Should work somewhat like this: 应该像这样工作:

SELECT *  
FROM TABLE_1 tb1
JOIN TABLE_4 tb4 ON tb1.pk1 = tb4.pk1
JOIN TABLE_U tbU ON tb4.UID = tbU.UID
LEFT JOIN TABLE_2 tb2 ON tb1.pk1 = tb2.pk1
LEFT JOIN TABLE_3 tb3 ON tb4.UID = tb3.UID

By the way, try listing all fields you actually need instead of using "SELECT *", since it's better for readability and performance. 顺便说一句,尝试列出您实际需要的所有字段而不是使用“SELECT *”,因为它更好的可读性和性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM