这是我的桌子,它很蹩脚但不要注意:
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 *
FROM
prices
WHERE
price_date IN (SELECT MAX(price_date) FROM prices WHERE price_id= ' . $it . ')';
price_id 对 item_id 的引用
但这是交易。
现在我希望应用程序显示订单时的商品价格(order_date),并且我在“价格”中有这样的价格历史记录:
2013-04-19 10:13:51 $2000
2013-03-21 11:15:56 $3000
2013-02-03 10:45:22 $1400
所以到目前为止,我只是从该表中选择了最大日期。你会如何建议我修改查询?(“价格”的最大日期时间,但小于或等于 order_date)
编辑:我想选择在订单(order_date 字段)时处于活动状态的价格值(价格字段)。所以如果 order_date = 2013-03-29 11:15:56 那么价格 = $3000 不是 $2000 也不是 $1400