如何将2个SQL Server表与查询连接并动态使用数据

时间:2021-09-13 11:17:23

I have two table in SQL Server; one is member table and the other is friend table.

我在SQL Server中有两个表;一个是成员表,另一个是朋友表。

  • In member table, we have information of members, like username password etc
  • 在会员表中,我们有会员信息,如用户名密码等

  • In friend table, we have userID and FriendID, so we can know each user how many friend have
  • 在朋友表中,我们有userID和FriendID,所以我们可以知道每个用户有多少朋友

So with Entity Framework in ASP.NET:

因此,使用ASP.NET中的Entity Framework:

  • how can I write a query to get friends names and ID of one member?
  • 如何编写查询以获取一个成员的朋友姓名和ID?

Tables look like this

表格看起来像这样

An example of friend table, you can see user with ID = 1 has a few friend

朋友表的一个例子,你可以看到ID = 1的用户有几个朋友

2 个解决方案

#1


0  

Try joining like this:

尝试像这样加入:

select * from member a join friend b on a.id=b.userid

#2


0  

There are 2 options for you.

有2个选项供您使用。

You have to do join and you can do it in SQL query. Or you can make LINQ join

您必须进行连接,并且可以在SQL查询中执行此操作。或者你可以使LINQ加入

Either way you will need to use JOIN

无论哪种方式,您都需要使用JOIN

#1


0  

Try joining like this:

尝试像这样加入:

select * from member a join friend b on a.id=b.userid

#2


0  

There are 2 options for you.

有2个选项供您使用。

You have to do join and you can do it in SQL query. Or you can make LINQ join

您必须进行连接,并且可以在SQL查询中执行此操作。或者你可以使LINQ加入

Either way you will need to use JOIN

无论哪种方式,您都需要使用JOIN