Mysql数据库中的as和distinct的使用
生活随笔
收集整理的這篇文章主要介紹了
Mysql数据库中的as和distinct的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在我們介紹兩個關鍵詞的用法之前,必要先把數據環境準備好。
創建數據庫
create database python_test_1 charset=utf8;使用數據庫
use python_test_1;創建students表
create table students(id int unsigned primary key auto_increment not null,name varchar(20) default '',age tinyint unsigned default 0,height decimal(5,2),gender enum('男','女','中性','保密') default '保密',cls_id int unsigned default 0,is_delete bit default 0 );創建classes表
create table classes (id int unsigned auto_increment primary key not null,name varchar(30) not null );向students表中插入數據
insert into students values (0,'小明',18,180.00,2,1,0), (0,'小月月',18,180.00,2,2,1), (0,'彭于晏',29,185.00,1,1,0), (0,'劉德華',59,175.00,1,2,1), (0,'黃蓉',38,160.00,2,1,0), (0,'鳳姐',28,150.00,4,2,1), (0,'王祖賢',18,172.00,2,1,1), (0,'周杰倫',36,NULL,1,1,0), (0,'程坤',27,181.00,1,2,0), (0,'劉亦菲',25,166.00,2,2,0), (0,'金星',33,162.00,3,3,1), (0,'靜香',12,180.00,2,4,0), (0,'郭靖',12,170.00,1,4,0), (0,'周杰',34,176.00,2,5,0);向classes表中插入數據
insert into classes values (0, "python_01"), (0, "python_02");查看表中的所有數據
as起別名
as關鍵字在自連接查詢的時候,經常使用,非常重要
1.使用 as 給字段起別名
select id as 序號, name as 名字, gender as 性別 from students;
2.通過 as 給表起別名
表名.字段名
select students.id,students.name,students.gender from students;可以通過 as 給表起別名
select s.id,s.name,s.gender from students as s;distinct消除重復行
在查詢的時候,當有多條重復數據的時候,使用distinct可以消除重復行
例:查詢班級中學生的性別
select distinct gender from students;
總結下:
總結
以上是生活随笔為你收集整理的Mysql数据库中的as和distinct的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的with上下文管理器
- 下一篇: 可接受任意数量参数的函数