简体   繁体   English

MySQL-使用select和PDO的条件插入查询

[英]Mysql - conditional insert query with select and PDO

I am unable to understand on how to apply insert query with select statement: 我无法理解如何使用select语句应用插入查询:

I have gone through this question also: 我也经历了这个问题:

MySQL INSERT from a SELECT with PDO 使用PDO从SELECT进行MySQL INSERT

But where is the VALUES part?? 但是, VALUES部分在哪里?

Like I have this query to insert in Mysql and here I use Values also: 就像我在MySQL中插入此查询一样,在这里我也使用Values:

$db_conn->beginTransaction();
$query = $db_conn->prepare('INSERT INTO mytable (name, user_id) VALUES(:sname, :uid)');
foreach($UploadData AS $DataValue)
{
    $query->execute(array(':sname' => $DataValue['Name'],':uid' =>$_SESSION['uid']));
}
$db_conn->commit();

My motto is to check if the name exists with the same uid it shouldn't import the data otherwise it should. 我的座右铭是检查name是否与相同的uid存在,否则不应导入数据。 But Where are the values part :/ I am blind :P 但是价值观在哪里呢:/我是盲目的:P

EDIT1 : From MySQL INSERT from a SELECT with PDO EDIT1 :从MySQL INSERT使用PDO进行SELECT

How will this code block work if no VALUES is supplied? 如果未提供VALUES此代码块将如何工作?

$sql_enc = '
    INSERT INTO sessionid (enc_id, enc_pass, enc_date) 
        (SELECT AES_ENCRYPT(username, :aeskey), AES_ENCRYPT(pwd, :aeskey), DATE_ADD(NOW(), INTERVAL 15 SECOND) FROM users WHERE username = :username)
';
$res_enc = $pdo->prepare($sql_enc);
$res_enc->bindParam(':aeskey', $aeskey);
$res_enc->bindParam(':username', $username);
$res_enc->bindParam(':pwd', $username);
$res_enc->execute();
$res_enc = null;

There are two valid INSERT syntax: 有两种有效的INSERT语法:

INSERT 
    INTO `table` [(field1, field2)] 
    VALUES ( 'val1', 'val2' )

Or 要么

INSERT 
    INTO `table` [(field1, field2)] 
    SELECT 'val1', 'val2'

the selected columns are your value fields. 选定的列是您的值字段。

@comments: Replace: http://dev.mysql.com/doc/refman/5.5/en/replace.html @comments:替换: http ://dev.mysql.com/doc/refman/5.5/en/replace.html

Procedures: http://dev.mysql.com/doc/refman/5.6/en/create-procedure.html 程序: http : //dev.mysql.com/doc/refman/5.6/en/create-procedure.html

You are defining the parameters :sname and :uid in your loop. 您正在循环中定义参数:sname和:uid。 The method execute takes the params and "put them" inside your query before executing this one. 方法execute接受参数并在执行查询之前将其“放入”查询中。

On other words, the query is compiled when you call prepare() and the parameters are applied when you call execute(). 换句话说,查询在您调用prepare()时被编译,而参数在您调用execute()时被应用。

Edit: Ok I didn't understand. 编辑:好的,我不明白。

The query includes a "SELECT" part which gives the values to insert. 该查询包括一个“ SELECT”部分,该部分提供要插入的值。 With SELECT you must not write "VALUES", as the documentation says: 使用SELECT时,您不得编写“ VALUES”,如文档所述

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    SELECT ...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
    [, col_name=expr] ... ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM