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

MySQL SQL学习 - 第一篇章

时间:2016-01-20 15:55:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:mysql   sql   

学习过程中,请轻点喷。用markdown写的,居然没有格式,不知道为何。


# 1.distinct
    select distinct user_name from cwd_user;
    去重,不能部分使用distinct,针对所有行进行进行检索

# 2.limit
    select * from user_mapping limit 2,3;
    limit 2,3 : 2指mysql返回从行2开始的3行,不包括本身的第2行。

# 3.order by
    select user_name from cwd_user order by user_name;
    #排序
    desc  
    #降序
    select id,user_name,email_address from cwd_user order by id desc;
    select id,user_name,email_address from cwd_user order by user_name desc,email_address;
    #desc关键字只能应用到直接位于其前面的列名
    asc(ascending)
    #升序
    select id,user_name,email_address from cwd_user order by user_name asc,email_address;
    #PS:默认为升序,如不指定desc,asc,则假定为asc
    select id,user_name,email_address from cwd_user order by id desc limit 1;
    #取出Id列最大值
    #区分大小写与排序顺序:在MySQL中,对于文本性数据,默认A与a是相同的,如果需要进行排序,必须进行另外的设置。

# 4. where #
    select id,user_name,email_address from cwd_user where user_name=‘liubin‘;
    #操作符 = <> != < <= > >= between
    #between 4 and 10 从低值到高值,and关键字分隔

# 5. null #
    select id,user_name,email_address from cwd_user where email_address is null;
    #表示返回没有email_address的行,NULL 无值(no value),它与字段包含0,空字符串或仅仅包含空格不一样

# 6. and、or#
    select id,user_name,email_address from cwd_user where id < 9666589 and user_name < ‘f‘;
    #满足所有给定的条件的行
    select id,user_name,email_address from cwd_user where id < 9666589 or user_name < ‘f‘;
    #满足任一一个条件的行
    #PS:and与or,and优先计算,一般情况下,请用()明确地分组操作符

# 7. in #
    select id,user_name,email_address from cwd_user where id in (425992,425993) order by user_name desc;
    #where 子句中用来指定匹配的值的清单的关键字,功能与or相当

# 8. not #
    select id,user_name,email_address from cwd_user where id not in (425992,425993) order by user_name desc;
    #where子句中用来否定后跟条件的关键字

# 9. 通配符 #
## 1) like ##
    select id,user_name,email_address from cwd_user where user_name like ‘b%‘;

## 2)% 百分号 ##
    #表示任何字符出现的任意次数,区分大小写,如Jaa%
    #尾空格可能会影响通配符匹配,可在最后附加一个%,如%Jaa%。或用其它函数去掉尾部空格
    #NULL,无法匹配

## 3) _下划线 ##
    #表示只匹配一个字符

## 4) 使用通配符技巧 ##
    #不要过度使用通配符
    #使用通配符时,尽量不要在搜索模式的开始处使用,否则会很慢
    #仔细注意通配符的位置,如果放错位置,将不能给你返回正确的数据

# 10. 正则表达式 #
    regexp ‘表达式‘
## 1)解析 regexp与like     
    select prod_name from products where prod_name like ‘1000‘;
    Empty set (0.00 sec)
    select prod_name from products where prod_name regexp ‘1000‘;
    +--------------+
    | prod_name    |
    +--------------+
    | JetPack 1000 |
    +--------------+
    1 row in set (0.00 sec)
    #Like匹配整个列。被匹配的值在列值中出现了(如,aaa1000),like也不会返回它,除非使用通配符。
    #regexp在列值内进行匹配。被匹配的值在列值中出现了(如,aaa1000),regexp会返回。

正则表达式
#

    select prod_name from products where prod_name regexp ‘1000‘;
    # ^1000$ 表示以1开头,0结尾
    select prod_name from products where prod_name regexp ‘.000‘;
    # . 表示匹配任意字符
    select prod_name from products where prod_name regexp binary ‘JetPacK .000‘;
    # binary 区分大小写
    # [[:<:]] 词的开始
    # [[::>]] 词的结束


## 2) or ##
搜索两个串之一,可以使用 |
#
    select prod_name from products where prod_name regexp ‘1000|2000‘;
    # 查询包含1000或2000的行
    select prod_name from products where prod_name regexp ‘[123] ton‘;
    # [^123] 匹配以1,2,3开头的所有行
    select prod_name from products where prod_name regexp ‘1|2|3 ton(可不加)‘;
    # 匹配几个字符之一的行

## 3) 匹配范围 ##
    select prod_name from products where prod_name regexp ‘[1-5]‘;
    # 匹配所有包含1到5的行
    select prod_name from products where prod_name regexp ‘[1-5] ton‘;
    # 匹配所有以1-5行开头的行

PS:

    特殊字符转义,必须以\\为前导。如,\\- 表示查询-,\\. 表示查询.
    \\f 换页
    \\n 换行
    \\r 回车
    \\t 制表
    \\v 纵向制表

    匹配字符类,预定义字符集
    [:alnum:]
    [:alpha:]
    [:blank:]
    [:cntrl:]
    [:digit:]
    ... ....

## 4) 匹配多次出现 ##
    * 0个或多个匹配
    + 1个或多个匹配(等于 {1,})
    ? 0个或多个匹配(等于 {0,1})
    {n} 指定数目的匹配
    {n,} 不少于指定数目的匹配
    {n,m} 匹配数目的范围(m,不超过255)


# 11. 计算字段 #
### 1) 拼接字段 ###
    concat():此函数用来拼接两个列    
    select concat(vend_name,‘ {‘,vend_country,‘}‘) from vendors order by vend_name;
    #拼接串,即把多个串

### 2) 去行头行尾空格 ###
    RTrim() 右侧
    LTrim()    左侧
    Trim()    两边
    select concat(Trim(vend_name),‘ {‘,vend_country,‘}‘) from vendors order by vend_name;

### 3) 别名 ###
    AS:一个字段或值的替换名,用AS关键字赋予
    select concat(Trim(vend_name),‘ {‘,vend_country,‘}‘) AS vend_title from vendors order by vend_name;

### 4) 执行算术计算 ###
    + - * /

    

本文出自 “崛起” 博客,请务必保留此出处http://binbinwudi8688.blog.51cto.com/3023365/1736699

MySQL SQL学习 - 第一篇章

标签:mysql   sql   

原文地址:http://binbinwudi8688.blog.51cto.com/3023365/1736699

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