mysql - Comparing day and week prices and getting cheapest -


i'm trying lowest price of object. problem there can daily , weekly prices. when searching cheapest price have multiply day price time 7 , compare week price cheapest. can happen object has week prices or day prices (or no prices @ all).

btw: has such subselect query, cause have more queries following later.

pricetable id  price   type    oid 1   10      d       1 2   12      d       2 3   70      w       1 4   80      w       2  objects id  name 1   house1 2   house2 

this i'm using not working correctly. when day price*7 bigger week price still gives me day price.

select p.oid, p.price, p.id, p.type pricetable p inner join (     select oid, min(if(type="w",price, price*7)) price, id, type     pricetable          group oid  ) p2 on p.oid = p2.oid , p.id= p2.id 

your query should work minimum price. however, should written as:

select oid, min(case when type = 'w' price else 7*price end) price pricetable      group oid ; 

if want other values row minimum price, need more logic. how this?

select pt.*oid, min(case when type = 'w' price else 7*price end) price pricetable pt pt.id = (select pt2.id                pricetable pt2                pt2.oid = pt.oid                order (case when pt2.type = 'w' pt2.price else 7*pt2.price end)                limit 1               ); 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -