Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

MYSQL Query Problem

$
0
0
Hi,

i have two table :- Products_tbl and Productdiary_tbl

Note: Text content in the code blocks is automatically word-wrapped
Product_tbl data :-

product_id(PK) | name | update_date | price

1 | prod1 | 25-07-2013 | 10
2 | prod2 | 26-07-2013 | 11
3 | prod3 | 27-07-2013 | 12
4 | prod4 | 28-07-2013 | 13

Productdiary_tbl data :-

id (PK) | product_id | update_date | price
101 | 1 | 25-07-2013 | 9
102 | 2 | 24-07-2013 | 11
103 | 1 | 29-07-2013 | 12
104 | 3 | 25-07-2013 | 15
105 | 1 | 30-07-2013 | 18
106 | 2 | 31-07-2013 | 20
107 | 3 | 1-08-2013 | 40



i want to get price from Productdiary_tbl for latest date (so only one record i want ), if entry is not there then price will come from Product_tbl (if no record in subtable then take the record from main table) table like here product_id - 4 entry in not in Productdiary_tbl for that price will come from Product_tbl.

i used below query for get the data but i am not getting data :-

select products_tbl.id,products_tbl.PRICE from products_tbl
where products_tbl.id not in
(select productsdiary_tbl.id,productsdiary_tbl.price from productsdiary_tbl where productsdiary_tbl.datee = (select max(productsdiary_tbl.datee) from productsdiary_tbl))
union
select productsdiary_tbl.id,productsdiary_tbl.price from productsdiary_tbl where datee =(select max(productsdiary_tbl.datenew) from productsdiary_tbl));


Thanks
Anchit

Viewing all articles
Browse latest Browse all 13329

Trending Articles