Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Mysql IF条件进行多次插入_Mysql_Sql - Fatal编程技术网

使用Mysql IF条件进行多次插入

使用Mysql IF条件进行多次插入,mysql,sql,Mysql,Sql,我想在Mysql中使用以下条件 我有两个表格-tblPage,tblContent SQL应该按照以下顺序执行操作 检查tblPage中的page_name=“Home”是否正确,如果错误,请转至步骤2 在tblPage中插入一些值,并获取最后一个插入Id 使用最后一个插入Id(tblPage)在tbContent中插入一些值,然后 获取自己的最后一个insertid 使用(tbContent的)lastInsertId在tbContent中插入一些值,然后 获取自己的最后一个insertid

我想在Mysql中使用以下条件

我有两个表格-tblPage,tblContent

SQL应该按照以下顺序执行操作

  • 检查tblPage中的page_name=“Home”是否正确,如果错误,请转至步骤2
  • 在tblPage中插入一些值,并获取最后一个插入Id
  • 使用最后一个插入Id(tblPage)在tbContent中插入一些值,然后 获取自己的最后一个insertid
  • 使用(tbContent的)lastInsertId在tbContent中插入一些值,然后 获取自己的最后一个insertid
  • 继续往这边插
  • 步骤1检查指定列中是否存在任何特定值

    步骤2如果指定的
    value=“Home”
    存在,请跳过步骤,如果不存在,请在tblPage中插入一些值并获取最后一个插入Id

    步骤3:在同一个表中插入上一次插入的lastInsertId值

    这一步可以继续下去,在同一个表中插入一些其他行,每次插入都会得到 下一个的上次插入Id

    我认为MySql
    IFNULL、NULIF、LAST\u INSERT\u ID
    等都可以,但我无法通过

    我尝试过这个,但是IF条件仍然不存在。如果tblPage中存在name='home',我需要执行查询的其余部分

    BEGIN
    INSERT INTO 'tblPage' ('id','name','url') VALUES 
    (NULL,'index','home/index');
    SET @last_id_in_page = LAST_INSERT_ID();
    INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES 
    (NULL,@last_id_in_page,'home/index');
    SET @last_id_in_content = LAST_INSERT_ID();
    INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES 
    (NULL,@last_id_in_page,@last_id_in_content);
    SET @last_id_in_content = LAST_INSERT_ID();
    INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES 
    (NULL,@last_id_in_page,@last_id_in_content);
    COMMIT;
    

    要获取您可以使用的最后一个插入的id

    $id= mysql_insert_id(); 
    

    插入查询后,您将获得表中最后一个插入的id。

    如何检查列上某个值的可用性,如果为false,则将执行查询的其余部分