根据值改变SELECTED列的数量?

时间:2019-11-27 11:10:39

标签: python python-3.x sqlite select case

我有一个sqlite数据表格,其中有许多状态列,这些列会随着产品在不同阶段的移动而更新。我想基于列状态值是否== 1扩展数据表的选定列,例如,如果第一列状态== 1,我想选择column2,如果第三列状态== 1,我想扩展选择范围选择column4和column5,如果第六列的状态== 1,我想使用column7进一步扩展选择范围。否则,如果仅第一列状态== 1仅选择列2,或者如果第三列状态== 1,则仅应选择列4和列5,反之亦然。 sqlite有可能吗?
我的数据表:

'CREATE TABLE IF NOT EXISTS wetweighscan_tb\
                    wet_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\
                    emp_id INTEGER NOT NULL,\
                    printed INTEGER NOT NULL DEFAULT "0",\
                    dtetime_printed TEXT NULL,\
                    first_scan INTEGER NOT NULL DEFAULT "0",\
                    block_id INTEGER NULL,\
                    frm_id INTEGER NULL,\
                    nature TEXT NULL,\
                    firstscan_emp_datetime TEXT NULL,\
                    second_scan INTEGER NOT NULL DEFAULT "0",\
                    weight REAL NULL,\
                    secondscan_emp_datetime TEXT NULL,\
                    user_id INTEGER NOT NULL,\
                    entity_id INTEGER NOT NULL,\
                    longitude REAL NULL,\
                    latitude REAL NULL,\
                    syncstatus INTEGER NOT NULL DEFAULT "0"'

这是我到目前为止所拥有的:

SELECT wet_id,
       emp_id, 
       user_id,
       entity_id
       CASE printed 
           WHEN 0 
               THEN select dtetime_printed 
       CASE first_scan
           WHEN 0
               THEN select block_id, frm_id, nature, firstscan_emp_datetime, longitude, latitude
       CASE second_scan
           WHEN 0
              THEN select weight, secondscan_emp_datetime

           ELSE null 
       END Wetscan
FROM 
    wetweighscan_tb;

但这不起作用。

编辑:所以我从另一个角度看了它,并创建了我想一次运行的所有select查询,而在主键上没有重复:

select * from wetweighscan_tb 
    WHERE printed = 1 and first_scan = 1 and second_scan = 1;

select wet_id, emp_id, user_id, entity_id,printed,dtetime_printed from wetweighscan_tb 
    WHERE printed = 1 and first_scan = 0 and second_scan = 0;

select wet_id, emp_id, user_id, entity_id,printed,dtetime_printed, first_scan, block_id, frm_id, nature, firstscan_emp_datetime, longitude, latitude from wetweighscan_tb 
    WHERE printed = 1 and first_scan = 1 and second_scan = 0;

如何使这三个查询成为一个逻辑查询?

0 个答案:

没有答案