使用条件逻辑:检查记录是否存在;如果是,更新它,如果没有,创建它 [英] using conditional logic : check if record exists; if it does, update it, if not, create it

查看:101
本文介绍了使用条件逻辑:检查记录是否存在;如果是,更新它,如果没有,创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我问了这个问题,并得到了有关合并的信息(这在postgresql中不可用)。我愿意尝试建议的解决方法但我只是想了解为什么它不能做条件逻辑。

I asked this last night, and got information on merging (which is unavailable in postgresql). I'm willing to try the workaround suggested But I'm just trying to understand why it can't be done with conditional logic.

我已经澄清了这个问题,所以也许这样会更容易理解。

I've clarified the question a bit, so maybe this will be easier to understand.

将数据插入表中的查询。但它每次都创造了一个新的记录。有没有办法我可以检查如果行是有第一,然后如果是, UPDATE ,如果它不是 INSERT

I have a query that inserts data into a table. But it is creating a new record every time. Is there a way I can check if the row is there first, then if it is, UPDATE, and if it isn't INSERT?

$user = 'username';
$timestamp = date('Y-m-d G:i:s.u');
$check_time = "start"; //can also be stop
$check_type = "start_user"; //can also be stop_user

$insert_query = "INSERT INTO production_order_process_log (
   production_order_id, production_order_process_id, $check_time, $check_type)
VALUES (
 '$production_order_id', '$production_order_process_id', '$timestamp', '$user')";

这个想法是表将记录签入和签出值( production_order_process_log.start production_order_process_log.stop )。因此,在具有签出时间戳的记录之前,查询应该检查 $ production_order_process_id 是否已经存在。如果它存在,那么时间戳可以进入 stop $ check_type 可以 stop_user 。否则,他们可以保留开始 start_user

The idea is that the table will record check-in and check-out values (production_order_process_log.start and production_order_process_log.stop). So before a record with a check-out time stamp is made, the query should check to see if the $production_order_process_id already exists. if it does exist, then the timestamp can go into stop and the $check_type can be stop_user. Otherwise, they can stay start and start_user.

我基本上试图避免这个结果。

I am basically trying to avoid this result.

+----+---------------------+--------------------------------+--------------------+-------------------+-------------+-------------+
| id | production_order_id |   production_order_process_id  |        start       |        stop       | start_user  |  stop_user  |
+----+---------------------+--------------------------------+--------------------+-------------------+-------------+-------------+
| 8  | 2343                |   1000                         |  12 july 03:23:23  | NULL              | tlh         |  NULL       |
+----+---------------------+--------------------------------+--------------------+-------------------+-------------+-------------+
| 9  | 2343                |   1000                         | NULL               | 12 july 03:45:00  | NULL        |  tlh        |
+----+---------------------+--------------------------------+--------------------+-------------------+-------------+-------------+

非常感谢您帮助我说明了postgresql逻辑来完成这个任务。

Many thanks for helping me suss out the postgresql logic to do this task.

推荐答案

此问题和答案会对您感兴趣:在重复更新(postgresql)上插入,

This question and answer will be of interest to you: Insert, on duplicate update (postgresql)

基本上,使用两个查询(做选择,如果发现更新,否则插入),这不是最好的解决方案可以提供重复的插入),或者做上面的问题建议 - 使一个存储过程/函数来做它(这可能是最好的选择,最简单)。

Basically, either use two queries (do the select, if it's found update, otherwise insert), which is not the best solution (two scripts running simultaneously could give duplicate inserts), or do as the above questions suggests - make a stored procedure/function to do it (this is probably the best option, and easiest).

这篇关于使用条件逻辑:检查记录是否存在;如果是,更新它,如果没有,创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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