doutan5844 2014-04-11 13:03
浏览 24
已采纳

如果表1中存在值,则插入表2 mysql

For my ordering system, when an admin inserts an order i need to check if the customer he inserts for the order exists in my customer table.
Im trying something like this, but with no luck..

$sql = "IF EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')
THEN insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
values ('$customer', '$product', '$quantity', 'now()', '$note', '$employee')";

Error: syntax error in first line. What is wrong here?
And is this the right approach? Is there a better way?

  • 写回答

3条回答 默认 最新

  • doushan2311 2014-04-11 13:08
    关注

    you might want to do it "backwards" - insert if exists:

    $sql = "insert into `order` (customer_id, product, quantity, creation_time, order_note, order_employee)
    select '$customer', '$product', '$quantity', now(), '$note', '$employee'
    from dual
    where EXISTS(SELECT * FROM customer WHERE customer_id = '$customer')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python