Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解postgreSQL中插入时的条件有困难_Python_Postgresql_Ternary Operator - Fatal编程技术网

Python 理解postgreSQL中插入时的条件有困难

Python 理解postgreSQL中插入时的条件有困难,python,postgresql,ternary-operator,Python,Postgresql,Ternary Operator,我有以下代码: temp3=plpy.execute("""insert into tabl(warehouseid, timeo) values(%s,%s) returning tablid"""% (temp_warehouseid, 0 if not temp_timeo else temp_timeo if producttypeid==1 else 0)) 我不确定我是否理解该值是如何插入到timeo列的 代码0 if not temp_timeo else temp_ti

我有以下代码:

temp3=plpy.execute("""insert into tabl(warehouseid, timeo)  
values(%s,%s) returning tablid"""% 
(temp_warehouseid, 0 if not temp_timeo else temp_timeo  if producttypeid==1 else 0))
我不确定我是否理解该值是如何插入到timeo列的

代码0 if not temp_timeo else temp_timeo是可以理解的,但是如果producttypeid==1 else 0,我们还有另一个条件,我不确定它是如何覆盖以前的值的

我以前从未在SQL查询中见过这种编码

具体而言,在本例中:

temp_warehouseid = 1
temp_timeo = 2
producttypeid =5

这将插入值为1,0的行,我假设这是因为producttypeid=5?但我不明白它是如何工作的。

您的问题是关于python构造的

tmp = VALUE1 if CONDITION else VALUE2
timeo = VALUE1 if CONDITION1 else VALUE2 if CONDITION2 else VALUE3
如果该条件为真,则将值1分配给tmp。如果为false,则表示值2无效

在您的查询中,这个python构造使用了两次

tmp = VALUE1 if CONDITION else VALUE2
timeo = VALUE1 if CONDITION1 else VALUE2 if CONDITION2 else VALUE3
如果条件1为true,则选择值1。如果为false,则在计算条件2 else VALUE3时使用第二条语句VALUE2

因此,当temp_timeo=2时,条件not temp_timeo为false。 当producttypeid=5时,条件producttypeid==1为false。 因此选择最后一个值0。

plpy.execute是PL/Python-