[英]How would I pull data from two tables using one variable that both tables have?
Lets say I have a variable 'userid', I want to select from aspnet_Membership AND aspnet_AccountProfile tables. 假设我有一个变量'userid',我想从aspnet_Membership和aspnet_AccountProfile表中选择。 They both have the column userid, I just want to be able to make a statement like SELECT * FROM aspnet_AccountProfile, aspnet_Membership WHERE UserId=@UserId and it gets the records with the matching user id for BOTH tables.
它们都具有列userid,我只想能够像SELECT * FROM aspnet_AccountProfile,aspnet_Membership WHERE UserId = @ UserId这样的语句,就可以得到两个表具有匹配用户ID的记录。 how do I do this?
我该怎么做呢? Thank you!
谢谢!
That is called a JOIN
: 那就是所谓的
JOIN
:
There are several basic types of join based on what data exactly you want. 根据您想要的数据,有几种基本类型的连接。 These are related to set theory/relational algebra.
这些与集合论/关系代数有关。 I'll list the most common ones:
我将列出最常见的:
INNER JOIN 内部联接
Use this when you want to return every possible combination of rows where both tables have a matching UserId. 如果要返回两个表都具有匹配UserId的行的每个可能组合,请使用此选项。 Some rows in either table may not get returned in an inner join.
任何表中的某些行可能无法在内部联接中返回。
SELECT * FROM aspnet_AccountProfile INNER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Another way of writing an INNER JOIN (Which I wouldn't encourage if you want to understand joins) is: 另一种编写INNER JOIN的方法(如果你想了解连接我不鼓励):
SELECT * FROM aspnet_AccountProfile, aspnet_Membership
WHERE aspnet_AccountProfile.UserId = aspnet_membership.UserId
Of course, to select the specific UserId you want, you add a condition on either table eg: 当然,要选择所需的特定UserId,请在任一表上添加条件,例如:
AND aspnet_AccountProfile.UserId = @UserId AND aspnet_AccountProfile.UserId = @UserId
OR 要么
AND aspnet_Membership.UserId = @UserId AND aspnet_Membership.UserId = @UserId
Either of those two will work fine for an inner join. 这两个中的任何一个都适合内部联接。
LEFT OUTER JOIN LEFT OUTER JOIN
Use this when you want to return all rows from the first table in your query, and every combination where the UserId in the second table matches the first. 如果要从查询中的第一个表以及第二个表中的UserId与第一个表中的UserId匹配的每个组合返回所有行,请使用此选项。 Some rows in the second table (Membership, in this case) may not get returned at all.
第二个表中的某些行(在这种情况下为成员资格)可能根本不会返回。
SELECT * FROM aspnet_AccountProfile LEFT JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
You have to use the left column to narrow down your criteria in this case, or it will automatically get converted to an INNER JOIN. 在这种情况下,您必须使用左列缩小条件,否则它将自动转换为INNER JOIN。
WHERE aspnet_AccountProfile.UserId = @UserId 在哪里aspnet_AccountProfile.UserId = @UserId
RIGHT OUTER JOIN 右外连接
This is fairly uncommon, because it can usually be written as a LEFT outer join. 这是相当罕见的,因为它通常可以写为LEFT外连接。 It's like a left outer join, but all rows from the second table in the relation are returned instead of the first.
它就像一个左外连接,但返回关系中第二个表的所有行而不是第一个。
SELECT * FROM aspnet_AccountProfile RIGHT JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
FULL OUTER JOIN 完全外部加入
Use this if you need to relate all the rows with a matching UserId in AccountProfile to the corresponding rows in Membership, but also need to know which rows in either table don't have a match in the other one. 如果需要将AccountProfile中匹配的UserId的所有行与Membership中的相应行相关联,请使用此方法,但还需要知道两个表中哪些行在另一个表中没有匹配。
SELECT * FROM aspnet_AccountProfile FULL OUTER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Getting results for only a single user is a little trickier in a FULL OUTER JOIN. 在FULL OUTER JOIN中,仅为单个用户获取结果有点棘手。 You have to specify that a NULL or the correct value is okay in either table.
您必须在两个表中指定NULL或正确的值都可以。
Hi,
嗨,
you can do it by using 你可以通过使用
"SELECT * FROM aspnet_AccountProfile ap, aspnet_Membership m WHERE ap.UserId=m.UserId ANB ap.UserId=@UserId"
“SELECT * FROM aspnet_AccountProfile ap,aspnet_Membership m WHERE ap.UserId = m.UserId ANB ap.UserId=@UserId”
you can do this by inner join. 你可以通过内部联接来做到这一点。
Here is the example, 这是一个例子,
Select aspnet_Membership.*, aspnet_AccountProfile.* from aspnet_AccountProfile
inner join aspnet_Membership on aspnet_Membership.userid = aspnet_AccountProfile.userid
where aspnet_Membership.UserId=@UserId
This will get only the record whem userid is common in both the table. 这将仅获得两个表中通用的用户ID记录。
If you want to get the record which are in 1 table and may or may not be in other then you must user the left join 如果要获取在1个表中并且可能不在其他表中的记录,则必须使用左连接
that is 那是
Select aspnet_Membership.*, aspnet_AccountProfile.* from aspnet_AccountProfile
left join aspnet_Membership on aspnet_Membership.userid = aspnet_AccountProfile.userid
where aspnet_Membership.UserId=@UserId
You can use a Join. 您可以使用Join。
Something like: 就像是:
Select * from aspnet_AccountProfile INNER JOIN aspnet_Membership
ON aspnet_AccountProfile.UserId = aspnet_Membership.UserId
Where aspnet_AccountProfile.UserId = @UserId
Thanks, 谢谢,
Vamyip Vamyip
您可以使用简单的内部联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.