简体   繁体   中英

How to join the column data of three Sql table using Join

I have four tables in which i have to join the column Data. I have used Cross join and at the end i get some values being repeated twice or several times. Please help.

The things i have tried in my sql script code is:

CREATE VIEW [dbo].[View_1]
AS
SELECT dbo.Table3.ID, dbo.Table4.Usertyp, dbo.Table4.reserved, dbo.Table2.Lotnumber, dbo.Table3.Current, dbo.Table3.Reading, dbo.Table1.LoginName, dbo.Table3.Start
FROM     dbo.Table1 CROSS JOIN
                  dbo.Table2 CROSS JOIN
                  dbo.Table3 CROSS JOIN
                  dbo.Table4
GO

Then when i execute i get the data entry several times, Am i doing something wrong. Please help. I needed a ID from Table 3, Usertyp from Table4 first row, reserved from table4, Lotnumber from Table2 first row data. How can i take all serial entry to a new table using four tables data. Thank you.

表格1 表2 表3 表4

The end result is. I wanted the ID from Table 3 and the respective data from the other table columns: The end output should look like, 表5

The problem is that you are using a cross join. Don't do that. That combines every row of every table in every combination. That can get ugly very fast.

The following should help get you there, however, there isn't enough info in what to match your table 4 to. So once you figure that out, you should be able to continue this approach I am listing:

CREATE VIEW [dbo].[View_1]
AS
SELECT   dbo.Table3.ID
       , dbo.Table4.Usertyp
       , dbo.Table4.reserved
       , dbo.Table2.Lotnumber
       , dbo.Table3.Current
       , dbo.Table3.Reading
       , dbo.Table1.LoginName
       , dbo.Table3.Start
FROM       dbo.Table3 t3
INNER JOIN dbo.Table1 t1 on t3.UserID = t1.ID
INNER JOIN dbo.Table2 t2 on t3.LotID = t2.LotID
INNER JOIN dbo.Table4 ....
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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