简体   繁体   English

如何在INSERT的另一列的值中使用表列的动态值

[英]How to use dynamic value of table column in the value of another column on INSERT

I'm doing something like the below.. 我正在做类似下面的事情。

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

Now as you can see for the duration, I am wanting to work on the resulting value of the start_time and end_time ; 现在,如您所见,在持续时间内,我想处理start_timeend_time的结果值; however obviously the code below will not work as that will work on the current value of the column I assume and won't use the resulting value I am wanting. 但是显然下面的代码将不起作用,因为它将对我假设的列的当前值起作用,并且不会使用我想要的结果值

Is there any way to do what I want? 有什么办法可以做我想要的吗?

You need to create a subquery with your computed values columns: 您需要使用您的计算值列创建一个子查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM