?
Solved

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

Posted on 2002-03-08
6
Medium Priority
?
158 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

770 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