如何从SQL中的两个不同表中选择值

时间:2022-03-01 14:15:05

I have two tables in my SQL Server database. The first is Test1 and second is Test2. There is a column RegNo in both tables.

我的SQL Server数据库中有两个表。第一个是Test1,第二个是Test2。两个表中都有一列RegNo。

Now I want to select the values from both the tables for a particular RegNo.

现在我想从两个表中为特定的RegNo选择值。

This is what I'm doing

这就是我正在做的事情

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1,Test2 JOINS
Test2 ON Test1.RegNo = Test2.RegNo

But my query is giving error.

但我的查询是错误的。

6 个解决方案

#1


6  

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1 
INNER JOIN Test2 ON Test1.RegNo = Test2.RegNo

#2


8  

select
    Test1.SurName,
    Test2.Class,
    Test2.Medium
from Test1
inner join Test2
on Test1.RegNo = Test2.RegNo

And if you want to select your data for a particular RegNo, just add a WHERE clause to the end, like so:

如果要为特定的RegNo选择数据,只需在末尾添加WHERE子句,如下所示:

select
    Test1.SurName,
    Test2.Class,
    Test2.Medium
from Test1
inner join Test2
on Test1.RegNo = Test2.RegNo
where Test1.RegNo = 123456   -- or whatever value/datatype your RegNo is

#3


3  

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1
INNER JOIN Test2
ON Test1.RegNo = Test2.RegNo

please see a visual explanation of joins this is very helpful in learning joins.

请查看联接的可视化解释,这对学习联接非常有帮助。

#4


1  

Try this:

尝试这个:

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1 INNER JOIN Test2
ON Test1.RegNo = Test2.RegNo
WHERE Test1.RegNo = desired_id

#5


1  

Very Basic question, try google next time and this now:

非常基本的问题,下次尝试谷歌,现在:

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1
inner join Test2 ON Test1.RegNo = Test2.RegNo

#6


1  

Here is your query with correction.

这是您的更正查询。

SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1 INNER JOIN Test2 ON Test1.RegNo = Test2.RegNo where  Test2.RegNo=Test1.RegNo;

Also you can filter the query by providing the RegNo,on whichEver table u want.

您也可以通过提供RegNo来过滤查询,您需要哪个表。

#1


6  

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1 
INNER JOIN Test2 ON Test1.RegNo = Test2.RegNo

#2


8  

select
    Test1.SurName,
    Test2.Class,
    Test2.Medium
from Test1
inner join Test2
on Test1.RegNo = Test2.RegNo

And if you want to select your data for a particular RegNo, just add a WHERE clause to the end, like so:

如果要为特定的RegNo选择数据,只需在末尾添加WHERE子句,如下所示:

select
    Test1.SurName,
    Test2.Class,
    Test2.Medium
from Test1
inner join Test2
on Test1.RegNo = Test2.RegNo
where Test1.RegNo = 123456   -- or whatever value/datatype your RegNo is

#3


3  

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1
INNER JOIN Test2
ON Test1.RegNo = Test2.RegNo

please see a visual explanation of joins this is very helpful in learning joins.

请查看联接的可视化解释,这对学习联接非常有帮助。

#4


1  

Try this:

尝试这个:

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1 INNER JOIN Test2
ON Test1.RegNo = Test2.RegNo
WHERE Test1.RegNo = desired_id

#5


1  

Very Basic question, try google next time and this now:

非常基本的问题,下次尝试谷歌,现在:

SELECT Test1.SurName, Test2.Class, Test2.Medium
FROM Test1
inner join Test2 ON Test1.RegNo = Test2.RegNo

#6


1  

Here is your query with correction.

这是您的更正查询。

SELECT Test1.SurName, Test2.Class, Test2.Medium
    FROM Test1 INNER JOIN Test2 ON Test1.RegNo = Test2.RegNo where  Test2.RegNo=Test1.RegNo;

Also you can filter the query by providing the RegNo,on whichEver table u want.

您也可以通过提供RegNo来过滤查询,您需要哪个表。