Solved

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

Posted on 2002-03-08
6
146 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 51

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 51

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 40 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

713 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