如果满足条件,则插入 [英] INSERT INTO if condition is satisfied

查看:85
本文介绍了如果满足条件,则插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当$solution[$i][0]属于查询SELECT num_arr FROM Schedule WHERE num_arr<>''的结果时,才需要将数组$solution保存到SQL表中.如果它不属于它,则必须不将行$ i保存在SQL表中.以下提供的查询似乎不正确.如何执行所描述的任务?正确的方法是什么?

I need to save the array $solution into the SQL table only if $solution[$i][0] belongs to the result of the query SELECT num_arr FROM Schedule WHERE num_arr<>''. If it does not belong to it, then the row $i must be not saved in the SQL table. The below-provided query seems to be incorrect. How do I implement the described task? What would be the correct approach to do this?

$query_land = "INSERT INTO `Sequence` (`num_arr`,`start`,`fin`,`way_id`) 
               VALUES ('".$solution[$i][0]."','".$solution[$i][1]."',
                                   '".$solution[$i][2]."','".$solution[$i][3]."') 
               WHERE '".$solution[$i][0]."' IN (SELECT num_arr 
                                                FROM Schedule 
                                                WHERE num_arr<>'')";

推荐答案

INSERT陈述有两个变体:

INSERT INTO tableX
    (a, b, c, ...)
VALUES
    (1, 2, 3, ...) ;

INSERT INTO tableX
    (a, b, c, ...)
SELECT
    1, 2, 3
FROM
    ... ;             --- the same or another table or many tables

dual是仅具有1行的系统表.它可以用于各种事物.在这里使用它是为了将VALUES (...)重写为SELECT ...,在该位置我们没有任何合适的表可放在FROM子句中:

The dual is a system table with exactly 1 row. It can be used for various things. Here it's used so a VALUES (...) is rewritten as a SELECT ... where we don't have any suitable table to put in the FROM clause:

$query_land = "INSERT INTO `Sequence` (`num_arr`,`start`,`fin`,`way_id`) 
               SELECT '".$solution[$i][0]."','".$solution[$i][1]."',
                      '".$solution[$i][2]."','".$solution[$i][3]."'
               FROM dual 
               WHERE '".$solution[$i][0]."' IN (SELECT num_arr 
                                                FROM Schedule 
                                                WHERE num_arr<>'')";

这篇关于如果满足条件,则插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆