php - How to select maximum of all values in datetime field but less or equal to other datetime value? -


here tables, it's pretty lame don't pay attention :

 create table items (  item_id int(11) not null auto_increment,  item_name varchar(255) not null,  item_cat int(11) not null,  item_desc text default null,  item_cost decimal(10, 0) default null,  item_image varchar(255) not null,  item_brand varchar(255) default null,  item_active int(11) not null,  primary key (item_id) )  create table prices ( price_id decimal(10, 0) not null, price_date datetime not null, price decimal(10, 0) default null, fake int(11) not null auto_increment, primary key (fake) )  create table orders ( order_id int(11) not null auto_increment, order_fam varchar(255) not null, order_name varchar(255) not null, order_otc varchar(255) default null, order_phone varchar(255) not null, order_email varchar(255) default null, order_delivery int(11) not null, order_payment int(11) not null, order_comment varchar(255) default null, order_status int(11) default null, order_adress varchar(255) default null, order_user_id int(11) default null, order_date datetime default null, primary key (order_id) )  mysql_query('set names utf8'); $query = 'select *                                                                                                  prices                                                                                                  price_date in (select max(price_date) prices price_id= ' . $it . ')'; 

price_id references item_id

but here's deal.

now want application show item's price @ moment of order (order_date) , have history of prices in "prices" this:

2013-04-19 10:13:51 $2000

2013-03-21 11:15:56 $3000

2013-02-03 10:45:22 $1400

so until selected max date table. how suggest me modify query? (maximum datetime of "prices" less or equal order_date)

edit: want select price value (price field) active @ moment of order (order_date field). if order_date = 2013-03-29 11:15:56 price = $3000 not $2000 , not $1400

i think query improved:

select *  prices                                                   order price_date desc limit 1; 

if add field "price_id" orders table , hook select whenever need bring pre price when order made:

select o.*, p.price orders o                                               inner join prices p on (o.price_id = p.price_id); 

i hope helps!


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -