•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,950 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 1819 | Replies: 8
![]() |
•
•
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation:
Rep Power: 4
Solved Threads: 19
Situation
table1 has 2 columns: userid and requiredInfo
It will have an entry for every user
table2 has 2 columns: userid and optionalInfo
It will have entries for some users but not others
Problem:
need a statement that returns either
userid, table1.requiredInfo, table2.optionalInfo
--if table2 has an entry
OR
userid, table1.requiredInfo, null
--if table2 does not have an entry
table1 has 2 columns: userid and requiredInfo
It will have an entry for every user
table2 has 2 columns: userid and optionalInfo
It will have entries for some users but not others
Problem:
need a statement that returns either
userid, table1.requiredInfo, table2.optionalInfo
--if table2 has an entry
OR
userid, table1.requiredInfo, null
--if table2 does not have an entry
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by campkev
Situation
table1 has 2 columns: userid and requiredInfo
It will have an entry for every user
table2 has 2 columns: userid and optionalInfo
It will have entries for some users but not others
Problem:
need a statement that returns either
userid, table1.requiredInfo, table2.optionalInfo
--if table2 has an entry
OR
userid, table1.requiredInfo, null
--if table2 does not have an entry
can be easy done with an outher join:
SELECT table1.userid, table1.requiredInfo, table2.optionalInfo
FROM table1 LEFT OUTER JOIN table2 ON table1.userid = table2.userid
•
•
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation:
Rep Power: 4
Solved Threads: 19
whoops, I messed up explaining the situation.
table2 is a many-to-many table with 3 columns, not 2
userid, optionalInfoDesc, OptionalInfoValue
what i need is
if Userid X has an entry in table2 with optionalInfoDesc = Y
return table1.requiredinfo, table2.optionInfoValue
if Userid X does not have an entry in table2 with OptionalInfoDesc = Y
return table1.requiredinfo, null
i tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
left outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that returns a row for every userid, not just userid X
then I tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
inner join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that only works if table2 has an entry
I tried
select table1.requiredInfo, table2.optionalInfoValue from table1
right outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that returns a row for every entry for userx in table2 not just the row with optionInfo Desc = y
where am I messing up
table2 is a many-to-many table with 3 columns, not 2
userid, optionalInfoDesc, OptionalInfoValue
what i need is
if Userid X has an entry in table2 with optionalInfoDesc = Y
return table1.requiredinfo, table2.optionInfoValue
if Userid X does not have an entry in table2 with OptionalInfoDesc = Y
return table1.requiredinfo, null
i tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
left outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that returns a row for every userid, not just userid X
then I tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
inner join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that only works if table2 has an entry
I tried
select table1.requiredInfo, table2.optionalInfoValue from table1
right outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y
but that returns a row for every entry for userx in table2 not just the row with optionInfo Desc = y
where am I messing up
•
•
Join Date: Nov 2005
Location: South Africa
Posts: 24
Reputation:
Rep Power: 3
Solved Threads: 0
Is it possible to post a screen shot of a diagram of the tables you are using?
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Originally Posted by campkev
nope, that returns one row for every user, not just the user specified. Any I solved the problem by using that select into a temp table and then doing a select from the temp table.
"where table1.userid = X " does definatelly NOT return any users which have NOT userid = X
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
- Wordpress, Joins and Indexes (MySQL)
- You Guys Play Videogames? (Geeks' Lounge)
- Sql Joins (MySQL)
- MS SQL Joins - newb assistance (MS SQL)
- Joins (MySQL)
- Problem with Rewriting Subqueries as Joins (Database Design)
- Problem with Rewriting Subqueries as Joins (MS SQL)
Other Threads in the MS SQL Forum
- Previous Thread: Default value of a column
- Next Thread: Get All Rows In Column In A Where Statment


Linear Mode