2017-12-02 333 views
0

我有一個包含三列的表:row,col,value。這是稀疏矩陣具有以下值:在表中插入行

Row-1 : 0, 1, 10 
Row-2 : 0, 3, 5 

的問題是,我怎麼能循環使用select語句來插入一個值0缺少的行和列?

+1

你使用了什麼[標籤:rdbms]? – Mureinik

+0

sqllite3做一些任務。 – Student

回答

1

如果你有所有列和所有行的列表,你可以這樣做:

insert into sparse (row, col, value) 
    select r.row, c.col, 0 
    from (select distinct row from sparse) r cross join 
     (select distinct col from sparse) c left join 
     sparse s 
     on s.row = r.row and s.col = c.col 
    where s.row is null; 

此版本假定每一行和col至少有一個值。

+0

非常感謝。有一個小錯字,最後一個條件是s.col = c.col。 – Student