Creating views based on two base tables

xiaoxiao2021-03-05  45

Based on the two base Table to establish a view, sometimes face a problem: two base tables are available in connection fields, but when they are not a fully mapping, that is, the records of the two tables are not one or one. If you use a simple connection to form a view, you will lose information. At this time we can choose to create a view. Specifically, there is a left connection, right connection, and all connections. You can choose to use according to the practicality. Example: (This is implemented under MS SQL Service) / * Create Database * / Create Table Uerw (Hao Int, Username Varchar (20), Primary Key (HAO)); Create Table Useri (Hao Int, UserInf Varchar (200 ), primary key (hao)); / * Insert data * / INSERT UERW VALUES (1, 'Gaoli'); Insert Uerw Values ​​(2, 'Liming'); Insert Uerw Values ​​(3, 'xiaogao'); Insert Useri VALUES (1, 'I am A student'); Insert Useri Values ​​(3, 'I am a Techer'); / * Create a view * / create view userinformation as select utterw.hao, UERW.USERNAME, Useri.userinf from Uerw LEFT JOIN USERI ON UERW.HAO = Useri.hao - Note For CREATE VIEW and process storage, it cannot be used by batch processing (that is, multiple statements). / * Query view * / select * from userInformation;

The results obtained are: 1, Gaoli, I am A Student2, Liming, Null3, Xiaogao, I am A Techer

转载请注明原文地址:https://www.9cbs.com/read-32995.html

New Post(0)