在學(xué)習(xí)java技術(shù)的時候,MySQL數(shù)據(jù)庫的基礎(chǔ)知識也會學(xué)到,那么小編本期為大家分享的java培訓(xùn)教程就是關(guān)于MySQL數(shù)據(jù)庫as和distinct關(guān)鍵字怎么用?代碼怎么寫?希望能幫助到同學(xué)們。
java培訓(xùn)教程之MySQL數(shù)據(jù)庫as和distinct關(guān)鍵字怎么用?代碼怎么寫?使用SQL語句顯示結(jié)果時,在屏幕顯示的字段名不具備良好的可讀性,我們可以使用 as 給字段起一個別名。在很多重復(fù)數(shù)據(jù)想要對其中重復(fù)數(shù)據(jù)行進(jìn)行去重操作可以使用 distinct。
as和distinct關(guān)鍵字怎么用?
學(xué)習(xí)目標(biāo):掌握去除重復(fù)數(shù)據(jù)行的關(guān)鍵字
1. as關(guān)鍵字
在使用SQL語句顯示結(jié)果的時候,往往在屏幕顯示的字段名并不具備良好的可讀性,此時可以使用 as 給字段起一個別名。
(1)使用 as 給字段起別名
select id as 序號, name as 名字, gender as 性別 from students;
(2)可以通過 as 給表起別名
-- 如果是單表查詢 可以省略表名
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通過 as 給表起別名
select s.id,s.name,s.gender from students as s;
說明:在這里給表起別名看起來并沒有什么意義,然而并不是這樣的,我們在后期學(xué)習(xí) 自連接 的時候,必須要對表起別名。
2. distinct關(guān)鍵字
distinct可以去除重復(fù)數(shù)據(jù)行
select distinct 列1,... from 表名;
例: 查詢班級中學(xué)生的性別
select name, gender from students;
-- 看到了很多重復(fù)數(shù)據(jù) 想要對其中重復(fù)數(shù)據(jù)行進(jìn)行去重操作可以使用 distinct
select distinct name, gender from students;
3. 小結(jié)
as 關(guān)鍵字可以給表中字段或表名起別名;distinct關(guān)鍵字可以去除重復(fù)數(shù)據(jù)行。
以上就是關(guān)于“MySQL數(shù)據(jù)庫as和distinct關(guān)鍵字怎么用?代碼怎么寫?”的相關(guān)教程介紹了,如果對于java培訓(xùn)課程相關(guān)知識還有具體想了解的問題,可以咨詢我們的客服小姐姐,他們會為您做一對一的解答哦。