使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句

Preparing a MySQL INSERT/UPDATE statement with DEFAULT values(使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句)
本文介绍了使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

引用 MySQL INSERT 手册 - 更新也一样:

Quoting MySQL INSERT manual - same goes for UPDATE:

使用关键字 DEFAULT 将列显式设置为其默认值.这使得编写将值分配给除少数列之外的所有列的 INSERT 语句变得更容易,因为它使您能够避免编写不包括表中每一列的值的不完整 VALUES 列表.否则,您必须写出与 VALUES 列表中的每个值对应的列名列表.

Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list.

简而言之,如果我写

INSERT INTO table1 (column1,column2) values ('value1',DEFAULT);

插入一个将 column2 设置为其默认值的新行 - 无论它是什么 - .

A new row with column2 set as its default value - whatever it may be - is inserted.

但是,如果我在 PHP 中准备并执行语句:

However if I prepare and execute a statement in PHP:

$statement = $pdoObject->
    prepare("INSERT INTO table1 (column1,column2) values (?,?)");
$statement->execute(array('value1','DEFAULT'));

新行将包含DEFAULT"作为其文本值 - 如果该列能够存储文本值.

The new row will contain 'DEFAULT' as its text value - if the column is able to store text values.

现在我已经为 PDO 编写了一个抽象层(我需要它),为了解决这个问题,我正在考虑引入一个

Now I have written an abstraction layer to PDO (I needed it) and to get around this issue am considering to introduce a

const DEFAULT_VALUE = "randomstring";

所以我可以执行这样的语句:

So I could execute statements like this:

$statement->execute(array('value1',mysql::DEFAULT_VALUE));

然后在进行绑定的方法中,我将检查发送给绑定的值,如果某些值等于 self::DEFAULT_VALUE,则采取相应的行动.

And then in method that does the binding I'd go through values that are sent to be bound and if some are equal to self::DEFAULT_VALUE, act accordingly.

我很确定有更好的方法来做到这一点.有没有其他人遇到过类似的情况?

I'm pretty sure there's a better way to do this. Has someone else encountered similar situations?

推荐答案

我知道的唯一解决方法"是使用 Coalesce() 和 默认(字段名)

The only "workaround" I know for this is to use Coalesce() and Default(fieldname)

例如

$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->exec("
  CREATE TEMPORARY TABLE foo (
    id int auto_increment,
    x int NOT NULL DEFAULT 99,
    y DATETIME NOT NULL DEFAULT '2010-03-17 01:00:00',
    z varchar(64) NOT NULL DEFAULT 'abc',
    primary key(id)
  )
");


$stmt = $pdo->prepare('
  INSERT INTO
    foo
    (x,y,z)
  VALUES
    (
      Coalesce(:x, Default(x)),
      Coalesce(:y, Default(y)),
      Coalesce(:z, Default(z))
    )
');
$stmt->bindParam(':x', $x);
$stmt->bindParam(':y', $y);
$stmt->bindParam(':z', $z);


$testdata = array(
  array(null, null, null),
  array(1, null, 'lalala'),
  array(null, '2009-12-24 18:00:00', null)
);
foreach($testdata as $row) {
  list($x,$y,$z) = $row;
  $stmt->execute();
}
unset($stmt);
foreach( $pdo->query('SELECT id,x,y,z FROM foo', PDO::FETCH_NUM) as $row) {
  echo join(', ', $row), "
";
}

印刷品

1, 99, 2010-03-17 01:00:00, abc
2, 1, 2010-03-17 01:00:00, lalala
3, 99, 2009-12-24 18:00:00, abc

这篇关于使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)