MySQL中使用表別名與字段別名

2021-08-28 18:43:47 字數 4082 閱讀 1606

mysql 表別名(alias)

sql 表別名

在 sql 語句中,可以為表名稱及欄位(列)名稱指定別名(alias),別名是 sql 標準語法,幾乎所有的資料庫系統都支援。通過關鍵字 as 來指定。

表別名語法:

select column from table as table_alias

上述 sql 執行後的效果,給人感覺是對 table_alias 表進行查詢,但實際上對單錶做簡單的別名查詢通常是無意義的。一般是對乙個表要當作多個表來操作,或者是對多個表進行操作時,才設定表別名。

下面是乙個簡單的多表操作的例子:

article 文章表: aid title content uid pubtime

user 使用者表: uid username password email regdate

當查詢一篇文章的時候,一般會同時將對應的文章作者查詢出來,通常的 sql 語句為:

select article.title,article.content,user.username from article, user

where article.aid=1 and article.uid=user.uid

設定表別名後:

select a.title,a.content,u.username from article as a, user as u where a.aid=1 and a.uid=u.uid

上述兩條 sql 語句查詢結果是一樣的

可以看出,使用表別名查詢,可以使 sql 變得簡潔而更易書寫和閱讀,尤其在 sql 比較複雜的情況下。除了使用別名來簡化 sql 外,有些時候例如乙個表做自身關聯時,必須要使用別名來當作兩個表進行關聯操作。

mysql 字段別名(列別名)

sql 字段別名

同本文前文講述的表別名一樣,sql(mysql) 也支援對錶的字段(列)設定別名。

字段別名語法:

select column as column_alias from table

字段別名使用例子

字段別名乙個明顯的效果是可以自定義查詢資料返回的欄位名。

在查詢的時候,對 username 字段使用別名:

select username as name,email from user

當然如此簡單的定義欄位的別名是沒有太大實際意義的,字段別名更多的意義是解決欄位名的重複,如乙個表字段被查詢兩次或更多次時:

select username as name,username,email from user

或者兩個及更多表進行查詢,有相同的返回欄位時:

兩個表都定義了 title 欄位且都需要返回該欄位資料時,就需要定義字段別名(至少定義乙個):

select a.title as atitle,u.username,u.title as utitle from article as a, user as u where a.uid=u.uid

可以看出,當查詢返回的欄位名稱相同時,可以通過定義別名來避免衝突,上面查詢的例子同時定義了字段別名與表別名。

提示通常,定義字段別名的 as 關鍵字可以省略,即下面兩句 sql 效果一致:

select username as name from user

select username name from user

但我們建議不要省略 as 關鍵字。

別名(alias)是 sql 的標準語法,幾乎所有的資料庫系統都支援。在處理一些複雜的查詢時,可以合理的定義表和字段別名來使 sql 語句看起來更加精簡易讀,也避免查詢返回相同字段資料時的衝突。

有如下sql語句:

【案例1】

select username as a,  useraddress as b from testtablewherealike』%am%』 

該語句執行是會報錯,因為別名只是對字段的一種引用,不能當作字段一樣使用,如果真要把別名當欄位一樣使用,可以如下變通:

select a, b

from(

select username as a,  useraddress as b from testtable

) as temptable 

where a like 『%dd%』 

如此,便可把別名當欄位一樣使用了

但是需要和足以紫色字型部分,該處要給內嵌的select查詢指定乙個別名,否則會報錯

【案例2】

select coutday as a, month1 as b,  (select 信用天數 from paydays where 自定義值 = month1) as c,  (a+ c) as 『total』 from testtable

變通為:

select a, b, a + c as  『total』 

from(

select coutday as a, month1 as b,  (select 信用天數 from paydays where 自定義值 = month1) as c from testtable

) as temptable

【案例3】

下面的sql語句中中,灰色部分重複了兩次,第二個灰色部分有不能用第乙個灰色區域的別名countdays替換,一旦替換會出現語法錯誤。

select 

ccustcode as 客戶**,

cpayway as 付款條件,  

cpaycode as 付款方式,

case

when cpayway like 『%月結%』 then (datediff(day, doutdate, dateadd(ms,-3,dateadd(mm, datediff(m,0,doutdate)+1, 0))) + (select 信用天數 from paydays where 自定義值 = cpayway))

else (select 信用天數 from paydays where 自定義值 = cpayway)

end as countdays,

convert(varchar(12),doutdate, 102) as 出庫日期,

convert(varchar(12),dateadd(day, cast(

(case

when cpayway like 『%月結%』 then (datediff(day, doutdate, dateadd(ms,-3,dateadd(mm, datediff(m,0,doutdate)+1, 0))) + (select 信用天數 from paydays where 自定義值 = cpayway))

else (select 信用天數 from paydays where 自定義值 = cpayway)

end) as int), doutdate), 102) as  實際回款日期

from ods.f_dispatchlist_all

where (isum-isnull(nsumaramt,0))>10e-8

我們把第二個灰色部分提出來,改為

select 客戶**,付款條件,  付款方式,countdays,出庫日期,convert(varchar(12),dateadd(day, cast(countdays)as int), doutdate), 102)as  實際回款日期 

from(

select 

ccustcode as 客戶**,

cpayway as 付款條件,  

cpaycode as 付款方式,

case

when cpayway like 『%月結%』 then (datediff(day, doutdate, dateadd(ms,-3,dateadd(mm, datediff(m,0,doutdate)+1, 0))) + (select 信用天數 from paydays where 自定義值 = cpayway))

else (select 信用天數 from paydays where 自定義值 = cpayway)

end as countdays,

convert(varchar(12),doutdate, 102) as 出庫日期

from ods.f_dispatchlist_all

where (isum-isnull(nsumaramt,0))>10e-8 

)

MySQL中使用表別名與字段別名的基本教程

mysql 表別名 alias sql 表別名 在 sql 語句中,可以為表名稱及欄位 列 名稱指定別名 alias 別名是 sql 標準語法,幾乎所有的資料庫系統都支援。通過關鍵字 as 來指定。表別名語法 select column from table as table alias 上述 sq...

MySQL為表和字段取別名

mysql在查詢資料時,可以為表和字段取別名,這個別名可以代替其指定的表和字段 查詢資料時,如果表名很長,使用起來不方便,此時,就可以為表取乙個別名,用這個別名來代替表的名稱 select from 表名 as 別名 注意,為表指定別名,as關鍵字可以省略不寫 為student表,取別名s,並查詢s...

MySQL為表和字段取別名

mysql在查詢資料時,可以為表和字段取別名,這個別名可以代替其指定的表和字段 查詢資料時,如果表名很長,使用起來不方便,此時,就可以為表取乙個別名,用這個別名來代替表的名稱 select from 表名 as 別名 注意,為表指定別名,as關鍵字可以省略不寫 為student表,取別名s,並查詢s...