Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
基于SQLite中的单行为一系列行切换位_Sql_Sqlite_Sql Update - Fatal编程技术网

基于SQLite中的单行为一系列行切换位

基于SQLite中的单行为一系列行切换位,sql,sqlite,sql-update,Sql,Sqlite,Sql Update,我有一个表,其中包含“header”或“normal”非header条目的行。这由整数关联列IsHeader跟踪 同样,如果行是“活动”的,我有一个列跟踪 通过一个表“Entries”和另一个列“MCL_Row”来查找相关行,我可以使用 UPDATE Entries SET(Active) = (SELECT (~(Active&1))&(Active|1) WHERE MCL_Row = <target>) WHERE MCL_Row = <

我有一个表,其中包含“header”或“normal”非header条目的行。这由
整数
关联列
IsHeader
跟踪

同样,如果行是“活动”的,我有一个列跟踪

通过一个表“Entries”和另一个列“MCL_Row”来查找相关行,我可以使用

UPDATE Entries SET(Active) = 
    (SELECT (~(Active&1))&(Active|1) WHERE MCL_Row = <target>) 
    WHERE MCL_Row = <target>;
通过表头设置S_Type=1激活后:

MCL_Row  S_Type  Active  IsHeader
1        1       1       1
2        1       1       0
3        1      >1<      0
4        2       1       1
5        2       1       0
6        2       1       0
MCL_Row  S_Type  Active  IsHeader
1        1      >0<      1
2        1      >0<      0
3        1       0       0
4        2       1       1
5        2       1       0
6        2       1       0
MCL_行S_类型活动IsHeader
1        1       1       1
2        1       1       0
3        1      >1<      0
4        2       1       1
5        2       1       0
6        2       1       0
通过标题将S_Type=1设置为非活动后:

MCL_Row  S_Type  Active  IsHeader
1        1       1       1
2        1       1       0
3        1      >1<      0
4        2       1       1
5        2       1       0
6        2       1       0
MCL_Row  S_Type  Active  IsHeader
1        1      >0<      1
2        1      >0<      0
3        1       0       0
4        2       1       1
5        2       1       0
6        2       1       0
MCL_行S_类型活动IsHeader
1        1      >0<      1
2        1      >0<      0
3        1       0       0
4        2       1       1
5        2       1       0
6        2       1       0
第一次查询

UPDATE  Entries 
SET     Active = 1-Active 
WHERE   MCL_Row = <target>
;








这似乎会更改标题行的值,但不会更改满足
S_Type=1
子句的其他行的值。我误解了吗?这是一个相关的子查询。从逻辑上讲,内部查询是基于外部查询的值执行的。这会将具有所需s_类型的行(标题和非标题)更改为标题的切换值感谢您的演示-这证实了我的想法-如果我希望它们具有相同的值(即,与“IsHeader”=1的行的新“Active”值相同,我应该运行您列出的第二个查询,并单独更新“IsHeader”行?我报告了一个SQLite错误
UPDATE  Entries

SET     Active =   (select    1-h.Active 
                    from      Entries as h 
                    where     h.S_Type   = Entries.S_Type 
                          and h.IsHeader = 1
                    )

WHERE   S_Type = <typenum>
create table Entries (MCL_Row int,S_Type int,IsHeader int,active int);

insert into Entries (MCL_Row,S_Type,IsHeader,active) values
    (1,123,1,1)
   ,(2,123,0,0)
   ,(3,123,0,0)
   ,(4,123,0,1)
;
select * from Entries;
+---------+--------+----------+--------+
| MCL_Row | S_Type | IsHeader | active |
+---------+--------+----------+--------+
| 1       | 123    | 1        | 1      |
+---------+--------+----------+--------+
| 2       | 123    | 0        | 0      |
+---------+--------+----------+--------+
| 3       | 123    | 0        | 0      |
+---------+--------+----------+--------+
| 4       | 123    | 0        | 1      |
+---------+--------+----------+--------+
UPDATE  Entries

SET     Active =   (select    1-h.Active 
                    from      Entries as h 
                    where     h.IsHeader = 1
                          and h.S_Type   = Entries.S_Type 
                    )
                    
WHERE   S_Type = 123 
;
select * from Entries;        
    
+---------+--------+----------+--------+
| MCL_Row | S_Type | IsHeader | active |
+---------+--------+----------+--------+
| 1       | 123    | 1        | 0      |
+---------+--------+----------+--------+
| 2       | 123    | 0        | 1      |
+---------+--------+----------+--------+
| 3       | 123    | 0        | 1      |
+---------+--------+----------+--------+
| 4       | 123    | 0        | 1      |
+---------+--------+----------+--------+
UPDATE  Entries

SET     Active =   (select    1-h.Active 
                    from      Entries as h 
                    where     h.IsHeader = 1
                          and h.S_Type   = Entries.S_Type 
                    )
                    
WHERE   S_Type = 123 
;
        
select * from Entries;        
+---------+--------+----------+--------+
| MCL_Row | S_Type | IsHeader | active |
+---------+--------+----------+--------+
| 1       | 123    | 1        | 1      |
+---------+--------+----------+--------+
| 2       | 123    | 0        | 0      |
+---------+--------+----------+--------+
| 3       | 123    | 0        | 0      |
+---------+--------+----------+--------+
| 4       | 123    | 0        | 0      |
+---------+--------+----------+--------+