dtlzdofl66441 2012-07-19 22:20
浏览 14
已采纳

使用条件逻辑:检查记录是否存在; 如果是,请更新它,如果没有,则创建它

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.

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')";

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        |
+----+---------------------+--------------------------------+--------------------+-------------------+-------------+-------------+

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

  • 写回答

2条回答 默认 最新

  • drdl18946 2012-07-19 23:27
    关注

    This question and answer will be of interest to you: Insert, on duplicate update in 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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)