热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

插入引用另一个表的值

我建议insertselect改用:insertintotest.products_per_order(Orders_id

我建议insert . . . select改用:

insert into test.products_per_order (Orders_idOrders, Products_idProducts, Price, Quantity)
select x.idOrder, x.idProduct, p.price, x.qty
from (select 1 as idOrder, 2 as idProduct, 5 as qty union all
select 1 as idOrder, 2 as idProduct, 4 as qty union all
. . . -- I've left these out so you can see the structure of the query
select 3 as idOrder, 3 as idProduct, 9 as qty
) x left join
test.products p
on p.idProducts = x.idProduct;

这使得错字导致错误价格的可能性大大降低。





推荐阅读
author-avatar
JohnBeanLee
A PHP Coder
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有