我有一系列SQL命令导致我正在使用php页面显示的表更新。
我想在每次加载页面时运行一堆sql语句,因为有一个日期字段会不时更新。有人可以建议最好的方法吗?
这是我当前的页面代码:
$sql = 'SELECT order_item_name, product_quantity,
order_number, category_name
FROM pvz8y_shopping_list';
我想包括以下内容:
DROP TABLE pvz8y_shopping_list;
CREATE TABLE pvz8y_shopping_list (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
isle_number int(11),
category_name char(180),
virtuemart_product_id int(11),
virtuemart_category_id int(11),
order_item_name char(255),
product_quantity int(11),
order_number char(64),
PRIMARY KEY (id)
);
INSERT INTO pvz8y_shopping_list (virtuemart_product_id, order_item_name, product_quantity, order_number)
SELECT virtuemart_product_id, order_item_name, product_quantity,order_number from pvz8y_virtuemart_order_items
INNER JOIN pvz8y_virtuemart_orders
ON pvz8y_virtuemart_order_items.virtuemart_order_id = pvz8y_virtuemart_orders.virtuemart_order_id
WHERE pvz8y_virtuemart_orders.created_on **>= '2014-07-19 00:00:00' AND
pvz8y_virtuemart_orders.created_on <= '2014-07-20 00:00:00';**
UPDATE pvz8y_shopping_list
INNER JOIN pvz8y_virtuemart_product_categories ON pvz8y_shopping_list.virtuemart_product_id = pvz8y_virtuemart_product_categories.virtuemart_product_id
SET pvz8y_shopping_list.virtuemart_category_id = pvz8y_virtuemart_product_categories.virtuemart_category_id;
UPDATE pvz8y_shopping_list
INNER JOIN pvz8y_virtuemart_categories_en_gb ON pvz8y_shopping_list.virtuemart_category_id = pvz8y_virtuemart_categories_en_gb.virtuemart_category_id
SET pvz8y_shopping_list.category_name = pvz8y_virtuemart_categories_en_gb.category_name;
正如您所见,使用sql语句创建shopping_list表,目前只显示结果,但我希望每次日期时都不必在后端创建表(以粗体突出显示更改)。
答案 0 :(得分:1)
嗯,我不知道您为什么要放弃并创建表格?如果只想清除表格,那么只需使用
即可 TRUNCATE TABLE pvz8y_shopping_list
MySQL有你可以使用的存储过程来完成你的工作,或者PHP也可以按时完成,但是必须在CRONTAB中添加脚本。
EDITED
定义连接
$hostname_appscon = "localhost";
$database_appscon = "DBNAME";
$username_appscon = "DBUSER";
$password_appscon = "DBPASS";
$appscon = mysql_pconnect($hostname_appscon, $username_appscon, $password_appscon) or trigger_error(mysql_error(),E_USER_ERROR);
现在使用上面定义的连接进行呼叫,您可以根据您的要求调用查询。
mysql_query("SQL QUERY1", $appscon);
mysql_query("SQL QUERY2", $appscon);
mysql_query("SQL QUERY3", $appscon);
这样您可以调用任意数量的查询