从 table 中的列中选择特定数字到另一个 table

selecting a specific number from a column in a table into another table

我有一个 table 叫做 processtime

id  | task1id | task1occ | task1time | task2id | task2occ | task2time | task3id | task3occ | task3time | task4id | task4occ | task4time | task5id | task5occ | task5time 
----+---------+----------+-----------+---------+----------+-----------+---------+----------+-----------+---------+----------+-----------+---------+----------+-----------
 10 |       9 |        0 |       300 |         |          |           |         |          |           |         |          |           |         |          |          
  7 |       5 |        1 |        20 |       6 |        1 |        45 |       1 |        0 |        60 |         |          |           |         |          |          
  9 |       6 |        1 |        45 |       7 |        1 |       120 |       2 |        0 |       110 |         |          |           |         |          |          
  8 |       5 |        1 |        20 |       6 |        1 |        45 |       3 |        1 |       200 |       1 |        0 |        60 |       4 |        1 |       300

和table调用了test1

 id | task1time | task2time | task3time | task4time | task5time 
----+-----------+-----------+-----------+-----------+-----------

我想做的是,如果 processtime 中任何 occ 列中的值 = 0,则任务时间显示在 test1 时间列中。

所以 test1 table 应该是这样的。 :

id  | task1time | task2time | task3time | task4time | task5time 
----+-----------+-----------+-----------+-----------+----------- 
 10 |    300    |           |           |           |     
 7  |           |           |     60    |           |
 9  |           |           |     60    |           |
 8  |           |           |           |     60    |

我试过使用 OR 和 AND 函数,但它们不起作用,我在想 IF 函数可能会起作用,但不知道从哪里开始,如果一个

非常感谢

戴夫

这是一个基本查询,应该可以按您的预期工作:

SELECT
    id,
    CASE WHEN task1occ = 0 THEN task1time END AS task1time,
    CASE WHEN task2occ = 0 THEN task2time END AS task2time,
    CASE WHEN task3occ = 0 THEN task3time END AS task3time,
    CASE WHEN task4occ = 0 THEN task4time END AS task4time,
    CASE WHEN task5occ = 0 THEN task5time END AS task5time
FROM yourTable;

如果您想用这些信息填充 test1 table,那么您可以使用 INSERT INTO ... SELECT,其中 SELECT 就是上面的查询。