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 > Filtering Result Rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-20-09, 06:33
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Filtering Result Rows

I am a MySQL beginner.
My table has the following columns:
Code:
Index | ProductID | CustID | GroupID | Price
Every ProductID can have many prices for Groups and for individual CustIDs. This querry
Code:
SELECT ProductID , Price
   FROM prices
   WHERE GroupID  = '2' || ( CustID  = '12' && GroupID   = '0' )
returns all the prices I need for "Group 2" + "Customer 12".
I do not want the rows from " GroupID = '2' " when I also have another result row matching " CustID = '12' && GroupID = '0' " for the same ProductID.

Can someone point me in the right direction?

Thanks
Reply With Quote
  #2 (permalink)  
Old 07-20-09, 06:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT ProductID
     , Price
  FROM prices
 WHERE CustID = 12 
   AND GroupID  = 0
UNION ALL
SELECT ProductID
     , Price
  FROM prices
 WHERE GroupID = 2
   AND NOT EXISTS
       ( SELECT 937
           FROM prices
          WHERE CustID = 12 
            AND GroupID  = 0 )
also, a couple of pointers in the right direction:

-- do not use those horrendous || and && abominations, but the standard SQL keywords OR and AND instead

-- do not put quotes around numbers used with numeric columns

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-20-09, 07:31
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks a lot r937,
I tried your suggestion.
One element seems to be missing in the " AND NOT EXISTS ( SELECT . . . " part.
Code:
SELECT ProductID
     , Price
  FROM prices
 WHERE CustID = 12 
   AND GroupID  = 0
UNION ALL
SELECT ProductID
     , Price
  FROM prices
 WHERE GroupID = 2
   AND NOT EXISTS
       ( SELECT Price
           FROM prices
          WHERE CustID = 12 
            AND GroupID  = 0
            AND ProductID = ?? )
The ?? stands for the ProductID of the curently checked row. How do I get the current ProductID instead of the ??
Reply With Quote
  #4 (permalink)  
Old 07-20-09, 07:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
try this --
Code:
SELECT ProductID
     , Price
  FROM prices
 WHERE CustID = 12 
   AND GroupID  = 0
UNION ALL
SELECT ProductID
     , Price
  FROM prices AS foo
 WHERE GroupID = 2
   AND NOT EXISTS
       ( SELECT 937
           FROM prices
          WHERE CustID = 12 
            AND GroupID  = 0  
            AND ProductID  = foo.ProductID )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 07-20-09 at 07:55.
Reply With Quote
  #5 (permalink)  
Old 07-20-09, 07:47
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Code:
SELECT ProductID
     , Price
  FROM prices
 WHERE CustID = 12 
   AND GroupID  = 0
UNION ALL
SELECT ProductID
     , Price
  FROM prices
 WHERE GroupID = 2
   AND NOT EXISTS
       ( SELECT Price
           FROM prices
          WHERE CustID = 12 
            AND GroupID  = 0
            AND ProductID = prices.ProductID )
does not work.
Isn't there a variable, expression or function in the MySQL language that returns a column value from the currently 'active' row?
Reply With Quote
  #6 (permalink)  
Old 07-20-09, 07:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vivoices
does not work.
well, yes -- because that's not what i suggested

please take another look, i have edited my previous post to highlight in colour what you should have coded
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-20-09, 08:07
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
Thanks a lot r937,
the 'AS foo ' escaped me. It does create a One Row temporary table within the statement, doesn't it?

Now the result comes back to PHP as I intended.
I was already thinking of coding the 'filter' in PHP, but I guess MySQL does a better job, performance-wise.

It will take me some time to get the hang of it,
your help is much appreciated.
Reply With Quote
  #8 (permalink)  
Old 07-20-09, 08:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by vivoices
It does create a One Row temporary table within the statement, doesn't it?
no, it doesn't

the second SELECT in the UNION has a correlated subquery

this means that the row inside the subquery needs to refer to a value outside the subquery, which is what the correlation variable or table alias called foo allows
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 07-20-09, 08:14
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
PHP Code:
$mySQLString"SELECT ProductID, Price FROM prices
 WHERE CustID = 
$MyCustomerID 
   AND GroupID  = 
$MyGroupID
UNION ALL
SELECT ProductID
     , Price
  FROM prices
 WHERE GroupID = 2
   AND NOT EXISTS
       ( SELECT Price FROM prices
          WHERE CustID = 
$MyCustomerID 
            AND GroupID  = 
$MyGroupID
            AND ProductID = 
$MyProductIDIWantTofind )"
PHP does a good job at spotting its variables inside a varaible assignment, however sometimes you may need to use string concatenation

eg
PHP Code:
$mySQLString"SELECT ProductID, Price FROM prices
 WHERE CustID = "
.$MyCustomerID.
   AND GroupID  = "
.$MyGroupID."
...undsoweiter 
remember if the column in the where clause is string/text/char then you need to encapsulate the parameter in quotes
eg
PHP Code:
$mySQLString"SELECT ProductID, Price FROM prices
 WHERE CustType = \""
.$MyCustomerID."\" 
   AND GroupType  = \""
.$MyGroupID."\"
...undsoweiter
;"

__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #10 (permalink)  
Old 07-20-09, 08:21
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
I see . . .
I find it very hard to get an understanding of MySQL by searching the Reference Manual.
Can you recommend some good online MySQL material that makes the language transparent for a PHP, AS3, Flex etc. coder?
Reply With Quote
  #11 (permalink)  
Old 07-20-09, 08:21
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Wouldn't it be more readable just to use single quotes on the strings ie for the last example:
PHP Code:
$mySQLString"SELECT ProductID, Price 
               FROM   prices
               WHERE  CustType = '
$MyCustomerID
                      AND GroupType  = '
$MyGroupID'
                      ...undsoweiter"

Reply With Quote
  #12 (permalink)  
Old 07-20-09, 08:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by healdem
PHP Code:
AND ProductID $MyProductIDIWantTofind )"; 
where does your $MyProductIDIWantTofind value come from?

i guess you missed most of the thread where we developed the concept of the correlated subquery

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 07-20-09, 08:31
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
@healdem
Building the filter in PHP would involve searching the entire dataset for each and every GroupID related price.
I would think that MySQL is more optimized than PHP searching an entire dataset 500 times.
Reply With Quote
  #14 (permalink)  
Old 07-20-09, 08:35
vivoices vivoices is offline
Registered User
 
Join Date: Jul 2009
Posts: 45
@r937
I will have a look at your book.
Reply With Quote
  #15 (permalink)  
Old 07-20-09, 08:50
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
Quote:
Originally Posted by r937
where does your $MyProductIDIWantTofind value come from?

i guess you missed most of the thread where we developed the concept of the correlated subquery

from a PHP variable set elsewhere in the PHP script
was I was trying to suggest is that such parameters can be set easily enough within the SQL string
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
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