Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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
Mysql 如何在插入时在另一列的值中使用表列的动态值_Mysql_Insert Into_Mysql Logic_Select Insert - Fatal编程技术网

Mysql 如何在插入时在另一列的值中使用表列的动态值

Mysql 如何在插入时在另一列的值中使用表列的动态值,mysql,insert-into,mysql-logic,select-insert,Mysql,Insert Into,Mysql Logic,Select Insert,我正在做下面的事情 INSERT INTO example_table (start_time, start_time_type, end_time, end_time_type, duration) SELECT IF(start_time_type = 'now' OR start_time < " . CURRENT_TIME . ",

我正在做下面的事情

INSERT INTO example_table
            (start_time,
             start_time_type,
             end_time,
             end_time_type,
             duration)
SELECT IF(start_time_type = 'now'
           OR start_time < " . CURRENT_TIME . ", " . CURRENT_TIME . ",
       start_time),
       start_time_type,
       IF(list_in = 'store', 0, ( IF(end_time_type = 'duration',
                                  " . CURRENT_TIME . " + duration * 86400,
                                  end_time
                                  ) )),
       IF(list_in = 'store', '', 'duration'),
       IF(list_in = 'store', end_time - start_time / 86400, duration)
FROM   bulk_listings
WHERE  .....
现在,正如您在持续时间中所看到的,我希望研究开始时间和结束时间的结果值;然而,很明显,下面的代码将不起作用,因为它将对我假设的列的当前值起作用,并且不会使用我想要的结果值


有什么方法可以满足我的要求吗?

您需要使用计算值列创建一个子查询:

SELECT start_time, start_time_type, end_time,
       IF(list_in = 'store', '', 'duration'),
       IF(list_in = 'store', end_time - start_time / 86400, duration)
FROM (SELECT IF(start_time_type = 'now'
           OR start_time < " . CURRENT_TIME . ", " . CURRENT_TIME . ",
       start_time) AS start_time,
       start_time_type,
       IF(list_in = 'store', 0, ( IF(end_time_type = 'duration',
                                  " . CURRENT_TIME . " + duration * 86400,
                                  end_time
                                  ) )) AS end_time,
       duration,
       list_in
FROM   bulk_listings
WHERE ...) AS SUBQUERY

你能再解释一下它是怎么工作的吗!?我看到了前5个选择,但为什么只有end_time_类型和duration通过IF结构获得它们的值?“开始时间”和“结束时间”不应该也这样做吗?我已经在子查询中添加了“持续时间”和“列表时间”字段,因为它们在主查询中是必需的。因此,子查询将计算新值start_time和end_time,并为其指定相同的名称。然后还将选择其他必填字段。主查询根据新值开始时间、结束时间进行计算。