MySQL查詢語句簡單操作示例
本文實例講述了MySQL查詢語句簡單操作。分享給大家供大家參考,具體如下:
查詢
創(chuàng)建數(shù)據(jù)庫、數(shù)據(jù)表
-- 創(chuàng)建數(shù)據(jù)庫
create database python_test_1 charset=utf8;
-- 使用數(shù)據(jù)庫
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
);
準備數(shù)據(jù)
-- 向students表中插入數(shù)據(jù) 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表中插入數(shù)據(jù) insert into classes values (0, "python_01期"), (0, "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常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
- 淺談pymysql查詢語句中帶有in時傳遞參數(shù)的問題
- MySQL模糊查詢語句整理集合
- MySQL查詢語句過程和EXPLAIN語句基本概念及其優(yōu)化
- PHP使用mysqli同時執(zhí)行多條sql查詢語句的實例
- mysql基礎架構教程之查詢語句執(zhí)行的流程詳解
- MySql帶OR關鍵字的多條件查詢語句
- Mysql帶And關鍵字的多條件查詢語句
- 詳解MySQL的limit用法和分頁查詢語句的性能分析
- php mysqli查詢語句返回值類型實例分析
- MySQL查詢語句大全集錦
- 最全的mysql查詢語句整理
- Oracle、MySQL和SqlServe三種數(shù)據(jù)庫分頁查詢語句的區(qū)別介紹
- 詳解MySQL 查詢語句的執(zhí)行過程
相關文章
mysql報錯1267?-?Illegal?mix?of?collations問題的解決方法
這篇文章主要介紹了mysql報錯1267?-?Illegal?mix?of?collations問題的解決方法,解決這個問題的方法是將兩個字符集統(tǒng)一起來,文中介紹了好幾種解決的辦法,需要的朋友可以參考下2025-01-01
Windows下MySQL?8.0.29?安裝和刪除圖文教程
這篇文章主要為大家詳細介紹了Windows下MySQL?8.0.29?安裝和刪除圖文教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
MySQL使用IF函數(shù)動態(tài)執(zhí)行where條件的方法
這篇文章主要介紹了MySQL使用IF函數(shù)來動態(tài)執(zhí)行where條件,詳細介紹了IF函數(shù)在WHERE條件中的使用,MySQL的IF()函數(shù),接受三個表達式,如果第一個表達式為true,而不是零且不為NULL,它將返回第二個表達式,需要的朋友可以參考下2022-09-09

