我正在尝试制作一个购物网站,在那里我出售每天价格波动的物品(贵金属)。我有一个表(products),它将包含每个产品的乘数(类似于"1.1")。基本上,我不想每天都要进入我的表格,改变数百种商品的价格。我的想法是创建另一个表,在这个表中,我只需每天用日值更改价格字段。如何使最终产品价格总和一个表中的乘数乘以另一个表中输入的日价格。或者,有比使用两个表更好的方法吗?以下是到目前为止的编码,只使用一个带有定义价格的表:
if (isset($_GET['id'])) {
//Connect To Mysql Database
include"storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i','',$_GET['id']);
//Use This VAR To Check To See If This ID Exists, If Yes Then Get Product
//Details, If No Then Exit Script and Give Message Why
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
//Get All The Product Details
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y",strtotime($row["date_added"]));
}
} else {
echo "That Item Does Not Exist";
exit();
}
} else {
echo "Data To Render This Page Is Missing";
exit();
}
mysql_close();发布于 2013-01-18 04:45:02
那么,如果一个响应不是通常的mysql_*相关的卖弄学问呢?
在下面的模式中,我将materials表从列出的价格中分离出来,以便可以根据日期存储它们。您可能会发现这对记录和/或开具发票很有用。
TABLE products
prod_id INT PK
prod_name VARCHAR
prod_price DECIMAL
mat_id INT FK
...
TABLE materials
mat_id INT PK
mat_name VARCHAR
...
TABLE material_pricing
mat_id INT FK PK
mat_price_date DATE PK
mat_price_factor DECIMAL
SELECT
p.prod_name,
p.prod_price * pr.mat_price_factor AS 'cur_price'
FROM products p INNER JOIN materials m
ON p.mat_id = m.mat_id
INNER JOIN material_pricing pr
ON m.mat_id = pr.mat_id
WHERE mat_price_date = TODAY()我正在尝试想一种方法来更改查询,以获取相关材料的最后一个定义的material_pricing条目,但我很难对子查询的数据进行排列……
编辑:这应该可以解决这个问题
SELECT
p.prod_name,
p.prod_price * pr.mat_price_factor AS 'cur_price'
FROM products p INNER JOIN materials m
ON p.mat_id = m.mat_id
INNER JOIN (
SELECT p1.*
FROM material_pricing p1 INNER JOIN (
SELECT mat_id, MAX(mat_price_date) 'mat_price_date'
FROM material_pricing
WHERE mat_price_date <= $target_date
GROUP BY mat_id
) p2
ON p1.mat_id = p2.mat_id
AND p1.mat_price_date = p2.mat_price_date
) pr
ON p.mat_id = pr.mat_id其中最内层的子查询中的$target_date将替换为今天的日期、TODAY() mySQL函数或所显示的发票的日期。
https://stackoverflow.com/questions/14387418
复制相似问题