If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > UPDATE with subquery ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-09, 13:53
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
UPDATE with subquery ?

For the life of me this query looks right but returns "0 row(s) affected" and I know there are 157 records which should match!

Nick

update CBA_Copy SET access_usr = 3
WHERE access_usr IS NULL AND CBA_ID =( SELECT MAX(CBA_ID) FROM DUES_PAID GROUP BY CBA_ID)
Reply With Quote
  #2 (permalink)  
Old 02-04-09, 15:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
in your subquery, you have GROUP BY cba_id

this means it will produce one aggregate row per cba_id

(that's what GROUP BY goes -- it aggregates a bunch of detail rows into one aggregate row)

naturally, for each distinct cba_d, the max(cba_id) is going to be that value!!!

it would be like asking what is the maximum employee id for each employee, where each employee has only one employee id

perhaps you could explain in words what you're trying to do?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-04-09, 15:32
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
In the CBA Table I need to set the access level to 3 if 1. the current access level is NULL and 2.the key(CBA_ID) has is listed in the DUES_PAID table. DUES PAID has multiple entries for this foreign key, one for each year they subscribed.

If there are no entries for the CBA_ID in the Dues_paid table then I need to set the access level to 2

Thanks
Nick


update CBA_Copy SET access_usr = 3
WHERE access_usr IS NULL AND CBA_ID =( SELECT MAX(CBA_ID) FROM DUES_PAID GROUP BY CBA_ID)
Reply With Quote
  #4 (permalink)  
Old 02-04-09, 16:06
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Would this be a better way of writing it?
Code:
update CBA_Copy 
SET    access_usr = 3
WHERE  access_usr IS NULL 
       AND exists( 
             select  1
             from    DUES_PAID dp
             where   dp.CBA_ID = CBA_Copy.CBA_ID )
You could set the access_usr to 2 in this SQL as well but it gets messy then.
Reply With Quote
  #5 (permalink)  
Old 02-04-09, 16:44
oldnickj oldnickj is offline
Registered User
 
Join Date: Jan 2009
Posts: 103
Exists? totally new to me but I found the reference in the very fine new book "Simply SQL" by Rudy Limeback.

I'll give it a try! Thanks
Reply With Quote
  #6 (permalink)  
Old 02-04-09, 16:50
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by oldnickj
Exists? totally new to me but I found the reference in the very fine new book "Simply SQL" by Rudy Limeback.
Never heard of him
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On