简体   繁体   English

SQL从另一个表列数据返回1个表数据

[英]SQL return 1 table data from another table column data

Hey guys quick question. 大家快问。 If i want to make a stored procedure to grab all information from 1 table from a different table column. 如果我想做一个存储过程来从另一个表列中的1个表中获取所有信息。 More detail..... table1 = users PK=accountid table2 = Account PK = accountid The row i want to check is called role(int only contains 1 and 0). 更多详细信息... table1 =用户PK =帐户table2 =帐户PK =帐户ID我要检查的行称为role(int仅包含1和0)。 so if role = 1 i want to check which accounts have role 1 and display all the users with that role number. 因此,如果角色= 1,我想检查哪些帐户具有角色1,并显示具有该角色编号的所有用户。 if not 1 then 0 will display the other users?? 如果不是1,则0将显示其他用户?

Now i was thinking along the lines of 现在我正在思考

 USE [database]
 GO

 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO
 CREATE PROCEDURE [dbo].[sp_Users_SelectAllByaccountRole]
 (
 @role int
 )
 AS

 BEGIN
 select * from Users 
 where (role = @role from Accounts) 
 && 
 (Users.accountid == account.accountid)

 END

But i do not know the syntax and i aint sure on my logic any help would be greatly appreciated. 但是我不知道语法,我也不希望对我的逻辑有任何帮助。

Assuming the Role column is on the Accounts table, then it seems like a simple INNER JOIN will do... 假设“ Role列在“ Accounts表上,那么看起来像是一个简单的INNER JOIN

SELECT u.*
FROM Users u
INNER JOIN Accounts a on a.AccountID = u.AccountID
WHERE a.Role = @role

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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