Solved

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

Posted on 2002-03-08
6
138 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 49

Expert Comment

by:Ryan Chong
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
> If the subselect is expected to return many rows, IN should be replaced by a JOIN
Agree with angellll :)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:louise_8
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 40 total points
Comment Utility
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
Comment Utility
Brilliant, thanks a million

Louise
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now