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 > Select if else

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-09, 10:07
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
Select if else

Hi i have a table named "prices"
it is like that

id prod_id price shopper_group
1---5-----10------1
2---5-----15------2
3---6-----20------1
4---7-----25------1

Some products have price for all the shopper groups some others not.

I d like to create a query that returns the prod_id of a certain shopper_group where price=myprice .
If there is not a price for that shopper_group return the product_id where price=myprice and shopper_group=1 (the default).

Is this possible through mysql?
Reply With Quote
  #2 (permalink)  
Old 12-02-09, 11:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by sakis View Post
Is this possible through mysql?
yes but you must specify which shopper_group you want ($mygroup) in addition to which price you want ($myprice)
Code:
SELECT COALESCE(selected.prod_id,default.prod_id) AS prod_id
     , COALESCE(selected.shopper_group,default.shopper_group) AS shopper_group
  FROM prices AS default
LEFT OUTER
  JOIN prices AS selected
    ON selected.price = default.price
   AND selected.shopper_group = $mygroup
 WHERE default.shopper_group = 1
   AND default.price = $myprice
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-03-09, 10:09
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
Thanks

but it doesn't return any result.

Code:
SELECT COALESCE(selected.product_id,def.product_id) AS prod_id ,
COALESCE(selected.shopper_group_id,def.shopper_group_id)
FROM jos_vm_product_price AS def 
LEFT OUTER JOIN jos_vm_product_price AS selected 
ON selected.product_price=def.product_price  
AND selected.shopper_group_id = 6 WHERE def.shopper_group_id = 1 AND def.product_price< 250

Any idea?
Reply With Quote
  #4 (permalink)  
Old 12-03-09, 10:28
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
It returns results now
The shopper_group_id=1 was deleted.

But it returns product_id's more than 1 times for all the user groups.

I used SELECT DISTINCT but has no effect

Is there a way to eliminate that?
Attached Thumbnails
Select if else-coalesce.jpg  

Last edited by sakis; 12-03-09 at 10:46.
Reply With Quote
  #5 (permalink)  
Old 12-03-09, 11:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by sakis View Post
But it returns product_id's more than 1 times for all the user groups.
you've got bad data
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-03-09, 11:29
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
what does this means ?
Can i do something for that ?

I am working on test server that has about 10 records in that table.
Reply With Quote
  #7 (permalink)  
Old 12-03-09, 11:30
sakis sakis is offline
Registered User
 
Join Date: Nov 2009
Posts: 19
what does this means ?
Can i do something for that ?

I am working on test server that has about 10 records in that table.
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