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 > workaround for a subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-07, 15:20
nbozic nbozic is offline
Registered User
 
Join Date: Mar 2004
Posts: 51
workaround for a subquery

Hi,

I'm connecting to a mySQL database that doesn't support subqueries (version 4.0.)...

The following is the short version of what I initially attempted to do:
-------------------------------
select
B.`Booking Number`,
B.`Offender ID`,
(
Select `Arrest Date` from Arrest
Where B.`Offender ID` = `Offender ID`
Order By `Arrest Date` desc
LIMIT 1
) as `Arrest Date`

FROM Booking B
where B.`Custody Status` = 'In Custody'
-------------------------------

As you can see, I want to return the respective arrest date per booking (most recent arrest date for each booking that has the person still in custody).

The problem is that the Booking and Arrest table DO NOT relate by "Booking Number", which is the unique identifier for the Booking table. Trying to do a regular join through `Offender ID` would return too many records which would repeat the "Booking Number".
(I want only one/unique "Booking Number" per record returned and avoid the growth of the recordset through joining)

How can I accomplish that without using a subquery?

I would appreciate any pointers.
Reply With Quote
  #2 (permalink)  
Old 01-05-07, 18:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select B.`Booking Number`
     , B.`Offender ID`
     , X.`Arrest Date` 
  from Booking as B
inner
  join Arrest as X
    on X.`Offender ID` = B.`Offender ID`
inner
  join Arrest as Y  
    on Y.`Offender ID` = B.`Offender ID`
 where B.`Custody Status` = 'In Custody'
group
    by B.`Booking Number`
     , B.`Offender ID`
     , X.`Arrest Date`
having X.`Arrest Date` = max(Y.`Arrest Date`)
order 
    By X.`Arrest Date` desc
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-08-07, 09:13
nbozic nbozic is offline
Registered User
 
Join Date: Mar 2004
Posts: 51
Exactly what I was looking for, thanks. I appreciate your help.
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