select

select 字段列表 from 数据表 [[as] 别名] [where 条件]

别名:

数据表 [[as] 别名]

select AA.money,BB.name from

product_offer_instance_object_xxx as AA ,

product_offer_instance_object_ZZZ BB

where AA.id = BB.id

字段名称 [[as]别名]

select product_offer_instance_object_id as ID,

product_offer_instance_object_name name,

coumn33 ‘金额’ from tablename;

select语句返回零条或多条记录;属于记录读操作

insert、update、delete只返回此次操作影响的记录数;属于写操作

常用select命令

使用select命令查看mysql数据库系统信息:

-- 打印当前的日期和时间

select now();

-- 打印当前的日期

select curdate();

-- 打印当前的时间

select curtime();

-- 打印当前数据库

select database();

-- 打印MySQL版本

select version();

-- 打印当前用户

select user();

--查看系统信息

show variables;

show global variables;

show global variables like '%version%';

show variables like '%storage_engine%'; 默认的存储引擎

like模糊搜索还可用户where字句,例如

select * from students where stname like '%l%1%2%3%';

除了like 还有not like

show engines;查看支持哪些存储引擎

--查看系统运行状态信息

show status;

show global status like 'Thread%';

多使用help

导出,导入数据库

导入数据库

导入数据库前必须创建一个空数据库

create database book;

Way 1:

mysql -uroot -p123456 book < book.sql

Way 2:

mysql> use book;

mysql> source /root/book.sql  #sql脚本的路径

导出数据库

导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名

mysqldump -u system -p123456 book>book2.sql

如何把一个select的结果导出到文本

select * into outfile '/tmp/123.txt' from books; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下

select name,ctfid,birthday,mobile,tel,email from info where ctfid like '130000%' into outfile '/tmp/fuping-of-rujia';

Sql查询语句进阶

在我们刚导入的book数据库进行测试

查看表的内容:

mysql> select * from category;

mysql> select * from books;

mysql> select * from books\G

查看字段类型:

desc 表名

mysql> desc books;

逻辑运算符:

and or not

and 且

or  或

not 非

选择出书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格

mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;

+--------------------------------------+--------------------------+-------+

| bName                                | publishing               | price |

+--------------------------------------+--------------------------+-------+

| Illustrator 10完全手册           | 科学出版社          |    50 |

| FreeHand 10基础教程              | 北京希望电子出版 |    50 |

| 网站设计全程教程             | 科学出版社          |    50 |

| ASP数据库系统开发实例导航 | 人民邮电出版社    |    60 |

| Delphi 5程序设计与控件参考  | 电子工业出版社    |    60 |

| ASP数据库系统开发实例导航 | 人民邮电出版社    |    60 |

+--------------------------------------+--------------------------+-------+

算术运算符:

= 等于

<> 不等于  !=

> 大于

< 小于

>= 大于等于

<= 小于等于

in 运算符

IN 运算符用于 WHERE 表达式中,以列表项的形式支持多个选择,语法如下:

WHERE column IN (value1,value2,...)

WHERE column NOT IN (value1,value2,...)

Not in 与in相反

当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择。

找出价格大于60的记录

mysql> select bName,price from books where price>60;

找出价格为60的

mysql> select bName,price from books where price=60;

找出价格不等于60的

mysql> select bName,price from books where price<>60;

找出价格是60,50,70的记录

mysql> select bName,price from books where price in (50,60,70);

找出价格不是60,50,70的记录

mysql> select bName,price from books where price not in (50,60,70);

排序:

升序:order by “排序的字段” asc  默认

降序:oredr by “排序的字段” desc

mysql> select bName,price from books where price  in (50,60,70) order by price asc;

+--------------------------------------+-------+

| bName                                | price |

+--------------------------------------+-------+

| Illustrator 10完全手册           |    50 |

| FreeHand 10基础教程              |    50 |

| 网站设计全程教程             |    50 |

| ASP数据库系统开发实例导航 |    60 |

| Delphi 5程序设计与控件参考  |    60 |

| ASP数据库系统开发实例导航 |    60 |

mysql> select bName,price from books where price  in (50,60,70) order by price desc;

+--------------------------------------+-------+

| bName                                | price |

+--------------------------------------+-------+

| ASP数据库系统开发实例导航 |    60 |

| Delphi 5程序设计与控件参考  |    60 |

| ASP数据库系统开发实例导航 |    60 |

| Illustrator 10完全手册           |    50 |

| FreeHand 10基础教程              |    50 |

| 网站设计全程教程             |    50 |

多个字段排序

select bName,price from books where price  in (50,60,70) order by price desc,bName desc;

范围运算:

[not]between ....and....

Between and 可以使用大于小于的方式来代替,并且使用大于小于意义表述更明确

查找价格不在30到60之间的书名和价格

mysql> select bName,price from books where price not between 30 and 60 order by price desc;

注:

这里的查询条件有三种:between。。。and,or 和 in

(30,60) >30 and <60

[30,60] >=30 and <=60

模糊匹配查询:

字段名 [not]like '通配符'  ----》% 任意多个字符

查找书名中包括"程序"字样记录

mysql> select bName from books where bName like '%程序%';

不含有

mysql> select bName from books where bName not like '%程序%';

MySQL子查询:

概念:在select 的where条件中又出现了select

查询中嵌套着查询

选择 类型名为“网络技术”的图书:

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='网络技术');

选择类型名称为“黑客”的图书;

mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName='黑客');

Limit限定显示的条目:

SELECT * FROM table LIMIT [offset,] rows

偏移量 行数

LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1):

比如select * from table limit m,n语句

表示其中m是指记录开始的index,从0开始,表示第一条记录

n是指从第m+1条开始,取n条。

查出category表中第2条到第6行的记录。

首先2到6行有2,3,4,5,6总共有5个数字,从2开始,偏移量为1

mysql> select * from category limit 1,5;

+---------+--------------+

| bTypeId | bTypeName    |

+---------+--------------+

|       2 | 网站       |

|       3 | 3D动画     |

|       4 | linux学习  |

|       5 | Delphi学习 |

|       6 | 黑客       |

+---------+--------------+

查看所有书籍中价格中最低的三条记录

我们对所有记录排序以升序排列,取出前面3个来

mysql> select bName,price from books order by price asc limit 0,3;

+-----------------------------+-------+

| bName                       | price |

+-----------------------------+-------+

| 网站制作直通车       |    34 |

| 黑客与网络安全       |    41 |

| 网络程序与设计-asp |    43 |

我们将子查询和限制条目,算术运算结合起来查询

显示字段bName ,price ;条件:找出价格比电子工业出版社出版的书中最便宜还便宜。

针对这种查询,我们一步步的来,先找出电子工业出版社出版中最便宜的书

mysql> select bName,price from books where publishing="电子工业出版社" order by price asc limit 0,1;

mysql> select bName,price from books where price<(select price from books where publishing="电子工业出版社" order by price asc limit 0,1);

或者

多行子查询: all表示小于子查询中返回全部值中的最小值

mysql> select bName,price from books where price<all(select price from books where publishing="电子工业出版社");

连接查询:

以一个共同的字段,求两张表当中符合条件的并集。 通过共同字段把这两张表连接起来。

常用的连接:

内连接:根据表中的共同字段进行匹配

外连接分两种:左外连接、右外链接。

内连接

语法:

select 字段  from 表1 inner join 表2  on 表1.字段=表2.字段

内连接:根据表中的共同字段进行匹配

测试:创建一个school数据库,创建两个表插入测试数据

mysql> create database school;

mysql> create table student(sid int(4) primary key auto_increment, name varchar(50));

mysql> insert into student values(1,'张三'),(2,'李四'),(3,'王二麻子'),(4,'HA'),(5,'Tom');

mysql> create table grade(id int(4) primary key auto_increment, score varchar(20), sid int(4));

mysql> insert into grade (score,sid) values ('1567',2),('1245',3),('1231',4),('1234',5),('1243',6);

查询有的成绩的人的信息.

mysql> select student.*,grade.* from student,grade where student.sid=grade.sid;

mysql> select student.*,grade.* from student inner join grade on student.sid=grade.sid;

mysql> select student.*,grade.* from student join grade on student.sid=grade.sid;

使用表别名,简写SQL语句

mysql> select s.*,g.* from student as s inner join grade as g on s.sid=g.sid;

select b.score,a.name from grade b,student a where a.sid = b.sid

外连接

左连接: select  字段 from a表 left join b表  on 连接条件

a表是主表,都显示。

b表从表

主表内容全都有,从表内没有的显示null。

右连接:

select 字段 from a表 right join  b表 on 条件

a表是从表,

b表主表,都显示。

mysql> select * from student as s right join grade as g on s.sid=g.sid;

+------+--------------+----+-------+------+

| sid  | name         | id | score | sid  |

+------+--------------+----+-------+------+

|    2 | 李四       |  1 | 1567  |    2 |

|    3 | 王二麻子 |  2 | 1245  |    3 |

|    4 | HA           |  3 | 1231  |    4 |

|    5 | Tom          |  4 | 1234  |    5 |

| NULL | NULL         |  5 | 1243  |    6 |

右连接,可以多表连接

mysql> select * from student as s left join grade as g on s.sid=g.sid;

+-----+--------------+------+-------+------+

| sid | name         | id   | score | sid  |

+-----+--------------+------+-------+------+

|   1 | 张三       | NULL | NULL  | NULL |

|   2 | 李四       |    1 | 1567  |    2 |

|   3 | 王二麻子 |    2 | 1245  |    3 |

|   4 | HA           |    3 | 1231  |    4 |

|   5 | Tom          |    4 | 1234  |    5 |

+-----+--------------+------+-------+------+

聚合函数

函数:执行特定功能的代码块。

算数运算函数:

Sum()求和

显示所有图书单价的总合

mysql> select sum(price) from books;

+------------+

| sum(price) |

+------------+

|      10048 |

+------------+

avg()平均值:

求书籍Id小于3的所有书籍的平均价格

mysql> select avg(price) from books where bId<=3;

+------------+

| avg(price) |

+------------+

|    39.3333 |

+------------+

max() 最大值:

求所有图书中价格最贵的书籍

mysql> select bName,max(price) from books; 这种方法是错误的

+-----------------------+------------+

| bName                 | max(price) |

+-----------------------+------------+

| 网站制作直通车 |       7500 |

+-----------------------+------------+

mysql> select bName,price from books where price=(select max(price) from books);

+----------------------------------------+-------+

| bName                                  | price |

+----------------------------------------+-------+

| Javascript与Jscript从入门到精通 |  7500 |

+----------------------------------------+-------+

min()最小值:

求所有图书中价格便宜的书籍

mysql> select bName,price from books where price=(select min(price) from books);

+-----------------------+-------+

| bName                 | price |

+-----------------------+-------+

| 网站制作直通车 |    34 |

+-----------------------+-------+

count()统计记录数:

统计价格大于40的书籍数量

mysql> select count(*) from books where price>40;

+----------+

| count(*) |

+----------+

|       43 |

+----------+

Count()中还可以增加你需要的内容,比如增加distinct来配合使用

算数运算:

+ - * /

给所有价格小于40元的书籍,涨价5元

mysql> update books set price=price+5 where price<40;

给所有价格高于70元的书籍打8折

mysql> update books set price=price*0.8 where price>70;

字符串函数:

substr(string ,start,len) 截取:从start开始,截取len长.start 从1开始算起。

mysql> select substr(bTypeName,1,6)from category where bTypeId=10;

+-----------------------+

| substr(bTypeName,1,6) |

+-----------------------+

| AutoCA                |      本来是AutoCAD技术

+-----------------------+

截取汉字

mysql> select substr(bTypeName,8,10)from category where bTypeId=1;

+------------------------+

| substr(bTypeName,8,10) |

+------------------------+

| 应用                 |       windows应用

+------------------------+

concat(str1,str2,str3.....) 拼接。 把多个字段拼成一个字段输出

mysql> select concat(bName,publishing) from books;

mysql> select concat(bName,"-----",publishing) from books;

大小写转换

upper()大写

mysql> select upper(bname) from books where bId=9;

+---------------------------+

| upper(bname)              |

+---------------------------+

| DREAMWEAVER 4ǽɡµň¶Ľ |

+---------------------------+

这样转换中文会出现乱码

lower()小写

mysql> select lower(bName) from books where bId=10;

+-------------------------------+

| lower(bName)                  |

+-------------------------------+

| 3d max 3.0 创作效果百例 |

+-------------------------------+

日期

curdate():

curtime();

now();

mysql> select curdate(),curtime(),now();

+------------+-----------+---------------------+

| curdate()  | curtime() | now()               |

+------------+-----------+---------------------+

| 2015-10-14 | 00:07:02  | 2015-10-14 00:07:02 |

+------------+-----------+---------------------+

mysql> create table stdate (name char(8),birthday date);

mysql> insert into stdate values('HA',now()),('LB',curdate());

mysql> select * from stdate;

+------+------------+

| name | birthday   |

+------+------------+

| HA   | 2015-10-14 |

| LB   | 2015-10-14 |

2-15-MySQL进阶的更多相关文章

  1. mysql进阶练习

    一 .  MySQL进阶练习 /*==========================创建班级表=============================*/ CREATE TABLE class ( ...

  2. 【目录】mysql 进阶篇系列

    随笔分类 - mysql 进阶篇系列 mysql 开发进阶篇系列 55 权限与安全(安全事项 ) 摘要: 一. 操作系统层面安全 对于数据库来说,安全很重要,本章将从操作系统和数据库两个层面对mysq ...

  3. Bash脚本15分钟进阶教程

    转载: Bash脚本15分钟进阶教程 这里的技术技巧最初是来自谷歌的"Testing on the Toilet" (TOTT).这里是一个修订和扩增版本. 脚本安全 我的所有ba ...

  4. mysql进阶(二十九)常用函数

    mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...

  5. mysql进阶(二十八)MySQL GRANT REVOKE用法

    mysql进阶(二十八)MySQL GRANT REVOKE用法   MySQL的权限系统围绕着两个概念: 认证->确定用户是否允许连接数据库服务器: 授权->确定用户是否拥有足够的权限执 ...

  6. mysql进阶(二十七)数据库索引原理

    mysql进阶(二十七)数据库索引原理 前言   本文主要是阐述MySQL索引机制,主要是说明存储引擎Innodb.   第一部分主要从数据结构及算法理论层面讨论MySQL数据库索引的数理基础.    ...

  7. mysql进阶(二十六)MySQL 索引类型(初学者必看)

    mysql进阶(二十六)MySQL 索引类型(初学者必看)   索引是快速搜索的关键.MySQL 索引的建立对于 MySQL 的高效运行是很重要的.下面介绍几种常见的 MySQL 索引类型.   在数 ...

  8. mysql进阶(十六)常见问题汇总

    mysql进阶(十六)常见问题汇总 MySQL视图学习: http://www.itokit.com/2011/0908/67848.html 执行删除操作时,出现如下错误提示: 出现以上问题的原因是 ...

  9. 【转】MySQL— 进阶

    [转]MySQL— 进阶 目录 一.视图 二.触发器 三.函数 四.存储过程 五.事务 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需 ...

  10. MySQL进阶(视图)---py全栈

    目录 mysql进阶(视图)---py全栈 一.什么是视图? 二.视图的特性 三.视图的优点 四.使用场合 五.视图基本操作 六.案例 mysql进阶(视图)---py全栈 一.什么是视图? 视图是从 ...

随机推荐

  1. Oracle安装部署之RAC安装环境配置脚本

    #!/bin/bash#Usage:Log on as the superuser('root'),and then execute the command:#./1preusers.sh group ...

  2. Oracle开发 之 主-外键约束FK及约束的修改

    试验环境: 1)数据库版本:oracle 11.2.0.4 2)建表脚本:以scott的dept及emp表为基础. 父表:dept -- Create table create table DEPT ...

  3. Convolution and polynomial multiplication

    https://www.mathworks.com/help/matlab/ref/conv.html?s_tid=gn_loc_drop conv Convolution and polynomia ...

  4. 什么是Java序列化和反序列化,如何实现Java序列化

    1.概念 序列化:把Java对象转换为字节序列的过程. 反序列化:把字节序列恢复为Java对象的过程. 2.用途 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个 ...

  5. 11.Git分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  6. yum whatprovides 查找哪个包可以提供缺失的文件

    yum whatprovides 查找哪个包可以提供缺失的文件

  7. Navicat连接服务器上的Mysql数据库

  8. RMAN备份保留策略

    RMAN备份保留策略 定义备份保留策略有以下两种方式: 1.使用CONFIGURE RETENTION POLICY TO RECOVERY WINDOW命令. 例如:RMAN>CONFIGUR ...

  9. STL学习笔记--各种容器的运用时机

    如何选择最佳的容器类别? 缺省情况下应该使用vector.vector的内部结构简单,并允许随机存取,所以数据的存取十分方便灵活,数据的处理也够快. 如果经常要在序列的头部和尾部安插和移除元素,应采用 ...

  10. Educational Codeforces Round 56 Solution

    A. Dice Rolling 签到. #include <bits/stdc++.h> using namespace std; int t, n; int main() { scanf ...