MySQL查詢 1 基本查詢

2021-10-11 07:46:41 字數 1893 閱讀 7518

-- 建立資料庫

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期");

select * from 表名;

例:select * from students;

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

例:select name from students;

select id as 序號, name as 名字, gender as 性別 from students;
-- 如果是單錶查詢 可以省略表明

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基本查詢語句

資料庫操作的sql語句 show database create database 資料庫名 charset utf8 use 資料庫名 select database drop database 資料庫名 表結構操作的sql語句 show tables create table students ...

MySQL基本查詢操作

查詢員工表中涉及到的所有的部門編號 去重 select distinct department id from employees 查詢員工姓和名連線成乙個字段,並顯示為姓名 select concat last name,first name as 姓名 from employees 查詢多個值,...

MySQL 查詢語句 1

一 建立資料庫 1 create database test 建立資料庫test 2 show databases 檢視目前資料庫中可用的資料庫,缺省會有系統資料庫 3 use test 將test設定為目前操作的資料庫 4 show tables 顯示乙個資料庫中的所有表 5 show colum...