Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL- how to process a subquery returning more than one value

Posted on 2002-03-08
6
Medium Priority
?
162 Views
Last Modified: 2008-02-01
Hi

Im tryin to select a number of PK's of a table where a FK is of each row has to be derived using another query.
The following wont work as the subquery is returning a number of rows/values, so how do I get around this??

select user_lesson_id from TAB_user_lesson where user_course_id = (SELECT user_course_id FROM TAB_user_course WHERE course_id = 1) ' this select could return a number of values

any ideas please
0
Comment
Question by:louise_8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6849978
Try:

select user_lesson_id from TAB_user_lesson where user_course_id IN (SELECT user_course_id FROM TAB_user_course
WHERE course_id = 1) ' this select could return a number of values

Cheers
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6849985
If the subselect is expected to return many rows, IN should be replaced by a JOIN:

select d.user_lesson_id
from TAB_user_lesson L
JOIN TAB_user_course C
ON C.user_course_id = L.user_course_id
AND C.course_id = 1

CHeers
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 6850012
> If the subselect is expected to return many rows, IN should be replaced by a JOIN
Agree with angellll :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:louise_8
ID: 6850165
thanks angellll, just one more query could I use an Update in this way and how?
eg update user_lesson set field = 3 where user_lesson.user_course_id = user_course.user_course_id and etc..  

or is the only way to do the slect first and get the pk's then update using the pk??
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 160 total points
ID: 6850170
If you want to update when joining to another table:

UPDATE TAB_user_lesson
SET field = 3
FROM TAB_user_lesson L
JOIN TAB_user_course C
ON C.user_course_id = L.user_course_id
AND C.course_id = 1

CHeers
0
 

Author Comment

by:louise_8
ID: 6850332
Brilliant, thanks a million

Louise
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question