Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
fengqidan
Starting Member
3 Posts |
Posted - 2010-07-19 : 00:16:04
|
| i have 2 tables T and M,T table for all the guests and M only for whom has already registed, i want to select all the guests who havent registed and tables content as shown as followT (guests)ID Email UserName1 A B2 G XYZ3 GG G4 G G5 GG XYZandM (members)ID TID Email MemberName1 5 GG XYZI want to write a query to find out the ppl in T table who has neither same Email nor MemberName in M table: query like followselect t.* from dbo.T t left outer join M m on t.Email = m.Emailleft outer join M m2 on t.UserName = m2.MemberNamewhere m.Email is null and m2.MemberName is null;Results: ID Email UserName1 A B4 G GNOW i want to get the result of only ID Email UserName1 A B,because 4 G G is actually redundent coz Email G and UserName G may already exist in the T table |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-19 : 01:13:40
|
[code]select *from @T twhere t.Email <> t.UserNameand not exists ( select * from @M m where m.Email = t.Email or m.MemberName = t.UserName )[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
fengqidan
Starting Member
3 Posts |
Posted - 2010-07-19 : 01:38:39
|
quote: Originally posted by khtan
select *from @T twhere t.Email <> t.UserNameand not exists ( select * from @M m where m.Email = t.Email or m.MemberName = t.UserName ) KH[spoiler]Time is always against us[/spoiler]
i think ur answer is not what i want, firstly, not to show the 4th record ID Email UserName4 G Gis not because the email and username are same, is because from eitherthese two propertities we can infer that it may registed by the same ppl(because it has the same Email with the 2nd record in Guest table, also it has the same username with the 3 record in Guest table)ID Email UserName1 A B2 G XYZ3 GG G4 G G5 GG XYZ |
 |
|
|
naveengopinathasari
Yak Posting Veteran
60 Posts |
Posted - 2010-07-19 : 02:37:49
|
| --DROP TABLE #T--DROP TABLE #MCREATE TABLE #T (ID INT , Email Varchar(100), UserName varchar(100))INSERT INTO #T VALUES (1,'A','B')INSERT INTO #T VALUES (2,'G','XYZ')INSERT INTO #T VALUES (3,'GG','G')INSERT INTO #T VALUES (4,'G','G')INSERT INTO #T VALUES (5,'GG','XYZ')CREATE TABLE #M (ID INT ,TID INT , Email Varchar(100), MemberName varchar(100))INSERT INTO #M VALUES (1,5,'GG','XYZ')--Here is a Complex query.--Not Advisable to use this query since your requirement is also not normalized.--But will work, but not recommended for more data in the table will take long time.--Please test and let me know the issueSelect * FROM #T WHERE ID NOT IN( Select ID FROM #T WHERE UserName IN ( Select Email FROM #T WHERE UserName IN ( SELECT Email FROM #M UNION SELECT MemberName FROM #M ) UNION Select UserName FROM #T WHERE UserName IN ( SELECT Email FROM #M UNION SELECT MemberName FROM #M ) ) UNION SELECT ID FROM #T WHERE Email IN ( Select Email FROM #T WHERE UserName IN ( SELECT Email FROM #M UNION SELECT MemberName FROM #M ) UNION Select UserName FROM #T WHERE UserName IN ( SELECT Email FROM #M UNION SELECT MemberName FROM #M ) ))Lets unLearn |
 |
|
|
|
|
|
|
|