mysql總結

sql語句

//修復系統號字段
ALTER TABLE issue_item add `create_user_code` bigint(20) NOT NULL DEFAULT '-1' COMMENT '創建人系統號';
alter table issue_item change column  user_code issue_user_code  bigint(20) NOT NULL DEFAULT '-1' COMMENT '出現問題的系統號';


//刷之前的信息
update issue_item set user_name = '達爾文',create_user_code = 2004,ucid = 100000002004 where issue_id <= 96;

//增加評論數字段
ALTER TABLE issue_item add `comment_count` int(10) NOT NULL DEFAULT '0' COMMENT '評論數';

//刷入之前的數據
update `issue_item` i set comment_count =(
if((select count(entity_id) from `comment` c where comment_type = 'issue' and c.entity_id =  i.issue_id GROUP BY c.entity_id) is null,0,
(select count(entity_id) from `comment` c where comment_type = 'issue' and c.entity_id =  i.issue_id GROUP BY c.entity_id))
) where 1=1;

需求3:給新建的表添加字段,並刷入數據

ALTER TABLE city_dict 

add `scope_code` int(2) NOT NULL DEFAULT '0' COMMENT '1 south;2 north;0 沒有區域信息',
add`scope` varchar(20) NOT NULL DEFAULT '' COMMENT 'south 南區;north 北區';

添加的字段有默認值,所以刷入數據不是插入數據,而是更新數據

UPDATE city_dict c JOIN open_list_common_config o ON  c.city_code = o.city_code
  set c.scope_code =  (case o.scope
       WHEN 'south' THEN '1'
    ELSE '2'
    END),c.scope=o.scope;

注意點總結

(1)寫複合sql時,先寫裏面的sql,再寫外層的sql

(2)在建表時,自增id,狀態status,創建時間和更新時間是四個功能字段,在建表時,狀態字段應該默認爲有效,這樣在刪除時,採用軟刪除,即讓status=無效來刪除,如果直接物理刪除找不到記錄(看具體需求),所以對應的在查詢的時候,應該在where條件處加上status=有效

(3)更新字段一般是展示出來是沒有意義的,因爲只要對這條記錄的任何字段修改,更新時間就會變,那麼展示這個更新人的信息就是沒有意義的,人人往往更關心做了什麼操作,所以記錄更新某個字段的人的信息具有價值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章