前言
很久沒更博客了,markdown語法都快忘了,排版可能醜點,將就着看吧。
年前的時候,公司安排寫一個數據清洗,生成一張表,技術層面不復雜,主要就是邏輯上覆雜一些,各種update寫了1000多行,一共關聯10張表。那時候其實公司已經用了postgresql了,老的sqlserver也一直在用,當時想着sqlserver上寫一下,後面移植到postgresql就好了。但沒想到,這簡直是給自己挖了一個大坑,後面逐句調試才成功。
1、update中不可以有inner join等,連接條件要寫在where後面 。
update table1 set column = table2.colunm from table2,table3,table4
where table1.id = table2.id and table1.id = table3.id and table1.id = table4.id ...
2、表格中null值必須單獨使用is null 判斷,例如一個字段中只有1種顯示作爲標記。
id | name | student |
---|---|---|
1 | 張三 | 1 |
2 | 李四 | |
3 | 王五 | 1 |
4 | 趙六 |
圖中表user中student字段,1代表是學生,空代表不是學生,假設此字段爲varchar類型。
select * from user where student = '1' # 這是查詢所有學生
注意坑來了,在SQLserver中可以使用下面這個
select * from user where student != '1' # SQLserver完美執行
但是在postgresql中必須使用
select * from user where student is null # postgresql這樣纔可以查得到
雖然就這兩個小問題,但是基本上讓我把這1000多行重寫了一遍。。。