问题描述
我下面的表格中的数据如下
Table_Cell
Cell_id Column_id row_id SwitchCell
------------------------------
1122 11 22 1
1133 11 33 0
1144 11 44 1
5522 55 22 1
5533 55 33 1
5544 55 44 1
Table_column
column_id SwitchCol
11 ?
55 ?
Table_row
row_id Switchrow
22 ?
33 ?
44 ?
我需要更新Table_column中的SwitchCol和Table_row中的Switchrow
逻辑是
如果对于Table_column中的column_id,表Table_Cell中存在的Table_row中具有row_id的所有组合均为1,则使该column_id的SwitchCol = 1 类似地,对于Table_row中的一个id,Table_row与表Table_Cell中的Table_column中的id的所有组合均为1,然后将该row_id设为Switchrow = 1
例如:对于column_id = 55,其与表Table_Cell(5522,5533,5544)中的Table_row表的组合具有SwitchCell = 1,因此55的SwitchCol将为1, 但是11的SwitchCol将为0(因为(1122,1133,1144)的1,1在一种情况下处于关闭状态。
请在逻辑上帮助我。寻找sql查询
解决方法
我认为您只需要min()
和相关的子查询:
update table_column co
set switchcol = (
select min(ce.switchcell)
from table_cell ce
where ce.column_id = co.column_id
)