2011-02-09 54 views
-1

惠那裏獲取數據。我有3個表看起來像這樣:從SQL表

create table Users 
(
UserID int identity, 
UserName varchar(50) not null, 
UserAddress varchar(100) not null, 
UserZipCode int not null, 
UserTown varchar(50) not null, 
UserPhone int not null, 
Comments varchar(max), 
primary key (UserID) 
) 

create table Groups 
(
GroupID int identity, 
GroupName varchar(50) not null, 
GroupDiscription varchar(max), 
primary key (GroupID) 
) 

create table UserGroups 
(
UserID int not null, 
GroupID int not null, 
) 

最後一個是用戶和組之間的所有環節由表。
我需要從所有用戶在選定的組中獲得用戶數據。

可以隨時幫助我嗎?

回答

2

您的意思是這樣的?

SELECT Users.UserID, 
     Users.UserName, 
     Users.UserAddress, 
     Users.UserZipCode, 
     Users.UserTown, 
     Users.UserPhone, 
     Users.Comments 
FROM Users 
    INNER JOIN UserGroups ON Users.UserID = UserGroups.UserID 
WHERE UserGroups.GroupID = @suppliedGroupID 
+0

葉thx ..這就是我一直在尋找... – MrKanin 2011-02-09 16:45:52

+1

除了你永遠不會想要使用select *;正確開始不要學習糟糕的編程技術。只選擇你需要的最小字段。 – HLGEM 2011-02-09 16:47:38

0

這似乎是一個家庭作業問題,所以我不會給任何直接的答案,除非你證明你已經把在工作,並試圖(我需要至少看到你嘗試編寫查詢)。

我會建議看看在SQL中使用連接。這裏有一個很好的資源:http://www.w3schools.com/sql/sql_join.asp