码迷,mamicode.com
首页 > 数据库 > 详细

数据库简单查询总结

时间:2016-05-06 02:13:48      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

数据库的查询操作基本分类:
1.投影操作 2.选择操作 3.排序操作


投影操作:

SELECT 列名列表 FROM 表名;
SELECT * FROM 表名;(查询整个表)
SELECT 列名 AS 新列名 FROM 表名;(查询列显示新的列名)
SELECT DISTINCT 列名 FROM 表名;(排除重复数据)
SELECT CONCAT(列名1,‘-‘,列名2,‘-‘,列名3) AS ‘标题‘ FROM 表名;(字符串连接)
SELECT 列名 FROM 表名 LIMIT 开始序号,返回行数; (返回限定行查询)


选择操作:

SELECT 列名 FROM 表名 WHERE 条件;
SELECT 列名 FROM 表名 WHERE 条件 AND 条件;(和)
SELECT 列名 FROM 表名 WHERE 条件 OR 条件;(或)
SELECT COUNT(*)AS ‘标题‘ FROM 表名 WHERE 条件;(统计满足条件的个数)
SELECT 列名 FROM 表名 WHERE 条件 BETWEEN 上限 AND 下限;
SELECT * FROM 表名 WHERE age IN(11,13,14) ;(找出指定范围的)
SELECT * FROM 表名 WHERE age NOT IN(11,13,14)(找出不在指定范围的) ;

SELECT 列名 FROM 表名 WHERE 条件 LIKE ‘_%‘;(模糊查询)
转义符 ‘%50p%%‘ ESCAPE ‘p‘ 或者 ‘%50<Escape Char>%%‘ 或者 ‘%50[%%‘
后两个转义MySQL中不行

SELECT 列名 FROM 表名 WHERE 列名1 IS NOT NULL;(查询测试列名1不为空)
SELECT 列名 FROM 表名 WHERE 列名1 IS NULL;(查询测试列名1为空)


排序操作:

SELECT 列名 FROM 表名 ORDER BY 列名 ASC(升序);
SELECT 列名 FROM 表名 ORDER BY 列名 DESC(降序);


SQL的执行顺序:
1.FROM 2.WHERE 3.SELECT 4.ORDER BY

 

 

 

 

-- 简单查询语句的标准结构
-- select column1,column2,column3 from t_name where column1 > 18 order by column1 asc; -- desc

-- drop database demo;
-- create database demo;
-- use demo;
/*
create table student(
id int primary key auto_increment,
`name` varchar(255) not null,
age int,
gender char(2),
address varchar(255) default ‘成都市‘
)charset=utf8;
*/
/*
insert into student(name, age, gender) values
(‘张三‘, 17, ‘男‘),
(‘李四‘, 19, ‘男‘),
(‘王五‘, 23, ‘女‘),
(‘张月‘, 22, ‘男‘),
(‘戴维‘, 30, ‘男‘),
(‘何莎莎‘, 22, ‘女‘),
(‘王丹‘, 20, ‘女‘),
(‘刘唐远‘, 24, ‘男‘),
(‘于洋‘, 15, ‘男‘),
(‘聂凤涛‘, 39, ‘男‘);
*/
-- select id,name,age,gender from student where age >18 order by age,name,id;

-- select id as ‘编号‘, name as ‘学生姓名‘, age as ‘年龄‘, gender as ‘性别‘ from student;

-- select age+10 as ‘十年后多大‘, name as ‘姓名‘ from student;

-- update student set gender = ‘女‘ where id = 4;

-- select distinct age,gender from student; -- 过滤重复数据,多个列时,使用组合匹配

-- select concat(name,‘-‘,gender,‘-‘,age) as ‘学生信息‘ from student;

-- select id, name, age, gender from student limit 5;

-- select id, name, age, gender from student limit 4,2;

-- select * from student where name != ‘张三‘ or age > 18;

-- select * from student where age between 15 and 22;

-- select * from student where age in(15,18,22);

-- select * from student where age not in(15,22);

-- insert into student(name, age, gender, address) values(‘搜索‘, 80, ‘男‘, ‘成都[‘);

-- select id,name,address from student where address like ‘%a%%‘ escape ‘a‘;

select * from student where gender is null;

select * from student where name is not null;

 

数据库简单查询总结

标签:

原文地址:http://www.cnblogs.com/chenyuanqiu2008/p/5463998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!