SQL union關鍵字用法

2021-06-20 20:14:13 字數 1686 閱讀 7839

自己總結下吧。

1     對於目前專案來說,這種情況可以用到union:

比如我們有兩個支付介面,乙個是支付通,另外乙個是匯付。支付通的資金記錄(通過支付通訊道的充值,提現,收款,還款等記錄)存放在cd_account_history_a2裡面;匯付的資金記錄(通過匯付通道的充值,提現,收款,還款等記錄)存放在cd_account_history_a3裡面;

這樣的話,兩張表的結構是一致的,,即使不一致,我們也可以選取部分字段,用 union;

舉例:查詢使用者id號是10715的支付通的資金記錄:select * from cd_account_history_a2 where account_id=780;

查詢使用者id是10715的匯付的資金記錄:select * from cd_account_history_a3 where user_id=10715;

將10715使用者的所有資金記錄聯合起來:trac_id,amount,create_time 列欄位顯示:

select trac_id,amount,create_time  from cd_account_history_a2 where account_id=780

union  all  select trac_id,amount,create_time   from cd_account_history_a3 where user_id=10715;

如果顯示出來的按照時間順序排列:

可以有兩種做法: 

第一種:   select    *  from (select trac_id,amount,create_time  from cd_account_history_a2 where account_id=780

union  all  select trac_id,amount,create_time   from cd_account_history_a3 where user_id=10715)a  order by a.create_time ;

第二種 select trac_id,amount,create_time  from cd_account_history_a2 where account_id=780

union  all  select trac_id,amount,create_time   from cd_account_history_a3 where user_id=10715  order by create_time;(oder by  將前面那塊看成乙個整體,通常我們用第二種)備註:where不支援,group by支援;

2    另外一種情況,比如cd_user這張表存放的資訊太多。我們要把之後的資訊存放在cd_user1表裡面,這樣也用到 union;

3    兩張表查詢出來結果的疊加,也可以用union;

但是必須保證兩張表查詢的列數一致,列欄位名一致(不一樣的可以用別名代替,下面的例子user_real_name 用別名user_nick代替,跟第一張表中的名字保持一致 )

select user_nick     from cd_user where id=10715 union 

select user_real_name  as  user_nick   from cd_user_infor  where user_id=10715;

備註:其實3這種情況,我們通常通過兩次查詢即可。或者用in巢狀語句,這裡只是舉個例子,可以這樣使用;

4   一張表裡面用 union比較方便:

const關鍵字用法

1 const常量 如const int max 100 優點 const常量有資料型別,而巨集常量沒有資料型別。編譯器可以對前者進行型別安全檢查,而對後者只進行字元替換,沒有型別安全檢查,並且在字元替換時可能會產生意料不到的錯誤 邊際效應 2 const修飾類的資料成員 class a const...

restrict關鍵字用法

概括的說,關鍵字restrict只用於限定指標 該關鍵字用於告知編譯器,所有修改該指標所指向內容的操作全部都是基於 base on 該指標的,即不存在其它進行修改操作的途徑 這樣的後果是幫助編譯器進行更好的 優化,生成更有效率的彙編 舉個簡單的例子 int foo int x,int y 很顯然函式...

Delphi not 關鍵字用法

這裡有兩個問題.第乙個,not是乙個邏輯運算子,表示邏輯取反.如hasundo為true,not hasundo為false,反之,hasundo為false,not hasundo則為true 第二個,是if語句.它要求if後是個布林型別的值.如果該值為true,則執行後面的語句,否則不執行.這樣...