我试图在mysql中使用CREATE EVENT语法自动运行每日表更新,在测试中,我无法使语法工作。它确实出现在SHOW EVENTS下,但是没有更新表格。 这可以通过检查INFORMATION_SCHEMA.TABLES中的create_time来确认。
我已经阅读了mysql网站上的文档,以及无数的例子,并且相信我的脚本应该没有错,但不幸的是,情况并非如此。
我已在下面附上我的脚本以供参考:
CREATE EVENT update_customer_segmentation
ON SCHEDULE
EVERY 1 DAY
STARTS '2017-11-22 06:10:00' ON COMPLETION PRESERVE ENABLE
DO
drop table if exists db_qty;
create table db_qty
select cpf.entity_id as product_id,
cpf.db_sku,
cpf.sku,
cpf.name,
cpev.value as db_qty
from catalog_product_flat_1 cpf
join catalog_product_entity_varchar cpev
on cpev.entity_id = cpf.entity_id
where attribute_id = 225;
create index index1 on db_qty (product_id);
-- tx_rank
DROP TABLE IF EXISTS tx_rank; SET @cid =''; SET @rn1 =1; SET @del ='';
CREATE TABLE tx_rank
SELECT
entity_id,
created_at,
customer_id,
tx_rank
FROM
(
SELECT
entity_id,
created_at,
customer_id,
@rn1 := IF(@cid=customer_id, IF(@del=entity_id, @rn1, @rn1+1),1) AS tx_rank,
@cid := customer_id,
@del := entity_id
FROM
(
SELECT
sfo.entity_id,
sfoi.created_at,
sfo.customer_id
FROM sales_flat_order sfo, sales_flat_order_item sfoi
WHERE sfo.entity_id = sfoi.order_id and customer_id is not null
GROUP BY customer_id, entity_id
ORDER BY customer_id, created_at ASC
) A
) B;
CREATE INDEX idx ON tx_rank (customer_id);
CREATE INDEX idx2 ON tx_rank (entity_id);
-- sfoi_lite
drop table if exists sfoi_lite;
create table sfoi_lite
select i.item_id,
i.order_id,
t.tx_rank,
case when tx_rank = 1 then 1 else 0 end as first_tx,
i.parent_item_id,
s.customer_id,
case when s.customer_id in
(161285, 511959, 494475, 295576, 295572, 451501, 64)
then 1 else 0 end as wholesale,
i.created_at,
i.updated_at,
i.product_id,
i.product_type,
i.sku,
i.name,
cp.db_sku,
pc.category,
pc.supercategory,
i.row_total,
cp.cost as item_cost,
cp.cost * i.qty_shipped as row_cost,
i.qty_ordered,
i.qty_shipped,
q.db_qty,
i.qty_ordered * q.db_qty as units_ordered,
i.price as item_price,
s.total_paid as order_total,
i.weight as weight,
a.delivery_date,
s.status
from sales_flat_order_item i
join sales_flat_order s on s.entity_id=i.order_id
join aw_deliverydate_delivery a on s.entity_id=a.order_id
left join tx_rank t on i.order_id = t.entity_id
left join db_qty q on i.product_id = q.product_id
left join product_categories pc on i.product_id = pc.product_id
left join catalog_product_flat_1 cp on i.product_id = cp.entity_id
;
create index order_idx on sfoi_lite (order_id);
create index item_idx on sfoi_lite (item_id);
create index customer_idx on sfoi_lite (customer_id);
create index product_idx on sfoi_lite (product_id);
create index delivery_idx on sfoi_lite (delivery_date);
create index db_sku_idx on sfoi_lite (db_sku);