创建 MySQL table 并使用 monthname 函数自动填充列
Create the MySQL table and autofill a column using monthname function
在 MySQL 中创建 table 时不确定如何使用 monthname
函数。我要做的就是在 A 列中输入 时,B 列应根据 A 列中的输入用单词填写月份名称。
我正在使用简单的 html 形式将值 post 设为 MySQL。
用户填写 HTML 数据,例如名称、订单号、订单日期、数量和商品名称。
这些值 posted 到 MySQL table orderdata。
MySQL table 包含 id、name、orderno、order_date、order_month、数量和 item_name.
等列
希望 order_month 列根据 order_date 自动填充月份名称。
下面是我试过的语法
create table orderdata (id int, order_date date, order_month DEFAULT ON UPDATE MONTHNAME(order_date),quantity int,item_name varchar(50));
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'DEFAULT ON UPDATE MONTHNAME(order_date),quantity
int,item_name varchar(50))' at line 1
create table orderdata (id int, order_date date, order_month UPDATE test.orderdata SET month= MONTHNAME(order_date),quantity int,item_name varchar(50));
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'UPDATE test.orderdata SET month=
MONTHNAME(order_date),quantity int,item_name va' at line 1
非常感谢任何帮助
谢谢
您正在尝试在 table 定义中添加触发器的作业。
基本上 table 的创作应该是:
CREATE TABLE orderdata
(id INT, order_date DATE, order_month VARCHAR(15), quantity INT, item_name VARCHAR(50));
根据 MySQL Trigger 文档,您将有以下触发器来实现您的目的:
DELIMITER $$
USE `<DATABASE-NAME>`$$
CREATE DEFINER = CURRENT_USER TRIGGER `<DATABASE-NAME>`.`orderdata_BEFORE_INSERT`
BEFORE INSERT ON `orderdata` FOR EACH ROW
BEGIN
SET NEW.order_month = MONTHNAME(NEW.order_date);
END $$
DELIMITER ;
然后当您执行任何给定的 INSERT
时,它会自动为您添加月份。
示例:
INSERT INTO `orderdata`(`id`, `order_date`, `order_month`, `quantity`, `item_name`)
VALUES (3,NOW(),NULL,23,'First order');
结果是:
如果您有 MySQL 版本 5.7.6 及更高版本,您可以使用生成的列并且无需触发器。
创建table
CREATE TABLE orderdata (
id INT
, order_date DATE
, order_month VARCHAR(50) AS (MONTHNAME(order_date)) #generated column
, quantity INT
, item_name VARCHAR(50)
);
插入查询。
INSERT INTO orderdata (id, order_date, quantity, item_name) VALUES(1, '2016-12-25', 1, 'item 1');
结果
SELECT * FROM orderdata;
id order_date order_month quantity item_name
------ ---------- ----------- -------- -----------
1 2016-12-25 December 1 item 1
在 MySQL 中创建 table 时不确定如何使用 monthname
函数。我要做的就是在 A 列中输入 时,B 列应根据 A 列中的输入用单词填写月份名称。
我正在使用简单的 html 形式将值 post 设为 MySQL。 用户填写 HTML 数据,例如名称、订单号、订单日期、数量和商品名称。
这些值 posted 到 MySQL table orderdata。 MySQL table 包含 id、name、orderno、order_date、order_month、数量和 item_name.
等列希望 order_month 列根据 order_date 自动填充月份名称。
下面是我试过的语法
create table orderdata (id int, order_date date, order_month DEFAULT ON UPDATE MONTHNAME(order_date),quantity int,item_name varchar(50));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT ON UPDATE MONTHNAME(order_date),quantity int,item_name varchar(50))' at line 1
create table orderdata (id int, order_date date, order_month UPDATE test.orderdata SET month= MONTHNAME(order_date),quantity int,item_name varchar(50));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE test.orderdata SET month= MONTHNAME(order_date),quantity int,item_name va' at line 1
非常感谢任何帮助
谢谢
您正在尝试在 table 定义中添加触发器的作业。
基本上 table 的创作应该是:
CREATE TABLE orderdata
(id INT, order_date DATE, order_month VARCHAR(15), quantity INT, item_name VARCHAR(50));
根据 MySQL Trigger 文档,您将有以下触发器来实现您的目的:
DELIMITER $$
USE `<DATABASE-NAME>`$$
CREATE DEFINER = CURRENT_USER TRIGGER `<DATABASE-NAME>`.`orderdata_BEFORE_INSERT`
BEFORE INSERT ON `orderdata` FOR EACH ROW
BEGIN
SET NEW.order_month = MONTHNAME(NEW.order_date);
END $$
DELIMITER ;
然后当您执行任何给定的 INSERT
时,它会自动为您添加月份。
示例:
INSERT INTO `orderdata`(`id`, `order_date`, `order_month`, `quantity`, `item_name`)
VALUES (3,NOW(),NULL,23,'First order');
结果是:
如果您有 MySQL 版本 5.7.6 及更高版本,您可以使用生成的列并且无需触发器。
创建table
CREATE TABLE orderdata (
id INT
, order_date DATE
, order_month VARCHAR(50) AS (MONTHNAME(order_date)) #generated column
, quantity INT
, item_name VARCHAR(50)
);
插入查询。
INSERT INTO orderdata (id, order_date, quantity, item_name) VALUES(1, '2016-12-25', 1, 'item 1');
结果
SELECT * FROM orderdata;
id order_date order_month quantity item_name
------ ---------- ----------- -------- -----------
1 2016-12-25 December 1 item 1