我的表格数据如下。 我必须找到hh_num列刚刚打印出第三列的预期结果,如下所示。
$value_to_display = $dataArray['some_index_value']['value'];
我会写一个如下的查询。
当rel = 0时的情况hh_num与row_num相同 但是当rel<> 0然后hh_num将是row_num的最大值,其中rel = 0但小于当前行数。
row_num rel_no hh_num
1 0 1
2 6 1
3 8 1
4 0 4
5 0 5
6 0 6
7 2 6
8 3 6
9 6 6
10 0 10
答案 0 :(得分:1)
create temp table table1 (
row_num int
,rel_no int
,hh_num int
);
insert into table1
select 1, 0, null
union all
select 2, 6, null
union all
select 3, 8, null
union all
select 4, 0, null
union all
select 5, 0, null
union all
select 6, 0, null
union all
select 7, 2, null
union all
select 8, 3, null
union all
select 9, 6, null
union all
select 10, 0, null;
update table1 a set hh_num = sub.hh_num
from
(select h.rowid as rown, h.row_num, h.rel_no, max(p.row_num) as hh_num
from table1 h inner join
table1 p
on p.rel_no = 0 and p.row_num <h.row_num
group by h.rowid, h.row_num, h.rel_no) sub
where a.rowid=sub.rown;
update table1 set hh_num = row_num
where rel_no = 0;