插入还是更新? [英] INSERT OR UPDATE?

查看:54
本文介绍了插入还是更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在用PHP编写一个使用PostGres数据库的应用程序。

我注意到的一件事是应该/可能的是一行

SQL代码大约需要6行PHP。对我来说这看起来很浪费和多余。


这是我正在谈论的一个例子($ db是PDO已经
定义和创建)。


$ query [1] =" UPDATE my.table,SET somefield =''$ someval''WHERE

somecondition" ;;

$ query [2] =" INSERT INTO my.table(somefield)VALUES(''$ someval'')" ;;

if(!$ db-> query($ query [1])){

$ db-> query($ query [2]);

}


我很想知道的是,如果有一些方法可以简化这一点,

要么购买一些PHP内置或扩展,要么可能是SQL中的东西

我不见了。在我看来,更新或插入应该是有效的,

但我似乎找不到任何相关的东西。


无论如何我希望你不介意,但我把它转到

pgsql.general和comp.lang.php,看看我能不能得到一些关于
的信息
主题。

Hello all,

I am writing an app in PHP that uses a PostGres database.
One thing i have noticed is that what should/could be a single line of
SQL code takes about 6 lines of PHP. This seem wasteful and redundant
to me.

Here is a sample of what I''m talking about ($db is a PDO already
defined and created).

$query[1] = "UPDATE my.table, SET somefield = ''$someval'' WHERE
somecondition";
$query[2] = "INSERT INTO my.table (somefield) VALUES (''$someval'')";
if(!$db->query($query[1])){
$db->query($query[2]);
}

What I''m curious to know is if there is some way to simplify this,
either buy some PHP builtin or extension, or possibly something in SQL
I am missing. It seems to me that "UPDATE OR INSERT", should be valid,
but I can''t seem to find anything relevant at all about it.

Anyways I hope you don''t mind, but I''m crossposting this to
pgsql.general and comp.lang.php to see if I can get some information on
the subject.

推荐答案

db已经定义并创建了一个PDO

)。

db is a PDO already
defined and created).


query [1] =" UPDATE my.table,SET somefield =''
query[1] = "UPDATE my.table, SET somefield = ''


someval'' WHERE

somecondition" ;;
someval'' WHERE
somecondition";


这篇关于插入还是更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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