我运行此代码以获取用户发票号,地址等列表。
SELECT A.id, A.userid, A.duedate, A.status, B.firstname, B.lastname, B.address1, B.city, B.postcode
FROM tblinvoices A
inner join tblclients B
on A.userid = B.id
WHERE duedate >= '2013-02-18'
AND duedate <= '2013-03-03'
AND A.status = 'Unpaid';
但我需要将一些信息插入另一个表格中。
现在我手动运行:
INSERT INTO `dbuser`.`tblnotes` (`id`, `userid`, `adminid`, `created`, `modified`, `note`, `sticky`) VALUES (NULL, '4585', '1', '2013-03-03 07:05:00', '', 'invoice by mail', '1');
如何实现自动化? 因此,第一个命令列出的所有“userid”只会添加一个说明,说明它是通过邮件发送的。
答案 0 :(得分:0)
使用INSERT INTO...SELECT
语句,ex
INSERT INTO dbuser.tblnotes (id, userid, adminid, created, modified, note, sticky)
SELECT NULL, A.userid, A.userid, '2013-03-03 07:05:00', '', 'invoice by mail', '1'
FROM tblinvoices A
inner join tblclients B
on A.userid = B.id
WHERE duedate BETWEEN '2013-02-18' AND '2013-03-03'
AND A.status = 'Unpaid';
注意:您必须按照INSERT
子句中列的顺序对齐select语句的值。