1. 程式人生 > 資料庫 >MySQL查詢語句簡單操作示例

MySQL查詢語句簡單操作示例

本文例項講述了MySQL查詢語句簡單操作。分享給大家供大家參考,具體如下:

查詢

建立資料庫、資料表

-- 建立資料庫
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,'小月月',1),'彭于晏',29,185.00,'劉德華',59,175.00,'黃蓉',38,160.00,'鳳姐',28,150.00,4,'王祖賢',172.00,'周杰倫',36,NULL,'程坤',27,181.00,'劉亦菲',25,166.00,'金星',33,162.00,3,'靜香',12,'郭靖',170.00,'周杰',34,176.00,5,0);

-- 向classes表中插入資料
insert into classes values (0,"python_01期"),"python_02期");

查詢所有欄位

select * from 表名;

例:

select * from students;

查詢指定欄位

select 列1,列2,... from 表名;

例:

select name from students;

使用 as 給欄位起別名

select id as 序號,name as 名字,gender as 性別 from students;

可以通過 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;

消除重複行

在select後面列前使用distinct可以消除重複的行

select distinct 列1,... from 表名;

例:

select distinct gender from students;

更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL查詢技巧大全》、《MySQL常用函式大彙總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧彙總》、《MySQL儲存過程技巧大全》及《MySQL資料庫鎖相關技巧彙總》

希望本文所述對大家MySQL資料庫計有所幫助。