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 > A complicated query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-09, 05:15
gonso75 gonso75 is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Question A complicated query

Hi

Im trying to create a rather complicated query and Im looking for asistance.

I have 2 tables A-B-C and I want to get all records from A that are related with B-C where a condition is met in C AND all of those that are not included in B.

I have two queries like this:

a) first query
SELECT *
FROM A, B, C
WHERE B.A_ID = A.ID
AND B.C_ID = C.ID
AND C.c_date > '2009-05-01 23:59:59'

b) second query
SELECT *
FROM A
WHERE A.ID NOT IN (SELECT DISTINCT B.A_ID)


I would like to mix both queries and I dont know how.
I have tried UNION but table A has some float fields and when I do the join that column goes crazy with decimal values:
0.3 appears as 0.300000011920929

I gues a left Join of shorts could work... but I cant figure it out.

Any help?


Thanks!
Gonso
Reply With Quote
  #2 (permalink)  
Old 06-12-09, 05:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
she's no possible

in the first query, you want A to match B --

... WHERE B.A_ID = A.ID

in the second query, you want A ~not~ to match B --

... WHERE A.ID NOT IN (SELECT DISTINCT B.A_ID)

therefore these queries cannot be combined

or else, if they are combined, it will never return any rows
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-12-09, 05:41
gonso75 gonso75 is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Quote:
Originally Posted by r937
she's no possible
It is possible, just like this:
SELECT *
FROM A, B, C
WHERE B.A_ID = A.ID
AND B.C_ID = C.ID
AND C.c_date > '2009-05-01 23:59:59'
UNION
SELECT *
FROM A
WHERE A.ID NOT IN (SELECT DISTINCT B.A_ID)

This returns the right set of records from A (its an kind of OR), but with float values going crazy.

Notice that the FK goes from B to A (not all the way around).

Im asking if there is another way, or a fix for the float problem....

help?
Reply With Quote
  #4 (permalink)  
Old 06-12-09, 06:48
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
The float problem is caused by the fact that computers store numbers as binary and some numbers (ie 1/3) simply don't store exactly in binary - see this article for 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