• mysql再探


    select子句及其顺序
    select from where group by having order by limit   

    创建表
    create table student(id int not null auto_increment,name varchar(20) default 'noname',age int,primary key(id)) engine=innodb;主键必须唯一,而且主键可以为多个,例如primary(id,name),auto_increment自增,default默认值,engine引擎,引擎有innodb,可靠的事物处理引擎,不支持全文本搜索,memory 数据存储在内存不是磁盘,速度快,适合临时表,myisam性能极高的引擎,支持全文本搜索,但不支持事物处理

    插入数据

    insert into student values(123,'linux',22);
    insert into student values(123,'linux',22),(11,'test',23);同时插入两条数据
    insert into student(id ,name,age) values(123,'linux',22);
    insert into student(id ,name) values(123,'linux');
    insert into student(id ,name,age) select id_cp,name_cp,age_cp from student_cp;插入检索出的数据


    更新数据
    update student set id=100 where name='test';
    update student set id=NULL where name='test';
    update student set id=100,age=22 where name='test';同时修改id和age

    删除数据
    delete from student where name='test';删除名字为test的那行
    更新表
    alter table student add addr char(20);给student表增加一个地址列

    altr table student drop column addr;删除student表地址列

    定义外键

    alter table orderitems add constraint fk_orderitems_orders  foreign key (order_num)  references orders (order_num); 其中constraint命名外键为fk-orderitems_orders,对应外键为order_num,链接的外表列为order_num

    删除表

    drop table student;

    重命名表

    rename table student to stuinfo;重命名为stuinfo
    rename table student to stuinfo,orders to dingdan;同时重命名两个表

    正则表达式的使用

    select  * from student where name regexp 'bp' order by id;找出名字包含bp的所有行

    select  * from student where name regexp 'bp.' ; 名字为bp且后面跟一个字符的学生,如果需要区分大小写,只需要在regexp后面加关键字binary
    select  * from student where name regexp 'bp|bp1|bp2';列出名字为bp或者bp1和bp2的
    select  * from student where name regexp 'bp[12]';列出叫做bp1以及bp2的学生,改成bp[1|2]也是一样的意思
    select  * from student where name regexp 'bp[^12]';列出不叫做bp1以及bp2的学生
    select  * from student where name regexp 'bp[1-9]';列出叫做bp1,bp2,bp3...的学生
    select  * from student where name regexp 'bp[a-z]';列出叫做bpa,bpb,bpc...的学生
    匹配特殊字符
    select  * from student where name regexp '\-';列出包含字符-的学生,\用来转义

    匹配字符类


        [:alnum:] 任意字符和数字[a-zA-Z0-9]
        [:alpha:]  任意字符[a-zA-Z]
        [:blank:]  空格和制表[\t]
        [:cntrl:]    ASCII控制字符,ASCII 0-31和127
        [:digit:]    任意数字[0-9]
        [:graph:]  与[:print:]相同,但不包括空格
        [:lower:]   任意小写字母[a-z]
        [:upper:]   任意大写字母[A-Z]
        [:print:]     任意可打印字符
        [:punct:]   既不在[:alnum:]又不在[:cntrl:]中的任意字符
        [:xdigit:]    任意十六进制数字[a-fA-F0-o]
        [:space:]   包括空格在内的任意空白字符[\f\n\r\t\v]


        匹配多个实例

       

        *            0个或多个匹配
        +           1个或多个匹配
       ?           0个或1个匹配
        {n}         指定数目匹配
        {n,}        大于或等于n次的匹配
        {n,m}     匹配数目为n到m
    select  * from student where name regexp '\([0-9] bp?\)';名字包含(1 bpa)之类的
    select  * from student where name regexp '[[:digit:]]{4}';名字包含4个数字的学生


        定位符

          ^                   文本的开始
          $                   文本的结束
          [[:<:]]             词的开始
          [[:>:]]             词的结束


          select  * from student where id regexp '^[0-9\.]';等价'^[0-9|\.]即数字开头或.开头,注意区分,如果^在[ ]里面,是否定该集合的意思,在[ ]外面就是开头的意思
    select concat(id,name,age) from student;把id和name以及age连起来成为一个字符串
    select trim(name) from student;去掉name左右的空格后输出,ltrim去掉左边空格,rtrim去掉右边空格
    select concat(id,name,age) as  linux from student;把列名改为linux
    select price,counts,price*counts as money from produces;增加一行统计价格并命名为money
    select upper(name) as linux from student;将名字转换为大写,lower()小写,left()返回串左边的字符,right()返回串右边的字符,locate()找出串的一个子串,soundex()返回串的soundex值,即发音差不多的,如lee和lie等等。


        时间和日期处理函数

        adddate()                     增加一个日期
        addtime()                     增加一个时间
        curdate()                      返回当前时间
        curtime()                      返回当前时间
        date()                           返回日期时间的日期部分
        datediff()                      计算两个日期之差

        day()  hour()   minute()   month()    now()   second()   year()   返回对应时间

        select * from orders where date(order_date)='2017-5-1' ;如果只知道查找某一天的,但是订单里的日期有包括了具体时刻,那么将其转换为date格式,就能找到相应日期的订单了

    select * from orders where date(order_date)='2017-5-1' and '2017-5-3';订单在该范围
    select * from orders where year(order_date)=2017 and month(order_date)=4;年月

        数值处理函数

        abs         绝对值
        cos         余弦值
        exp         指数值
        mod        余数
        pi            圆周率
        rand        随机数
        sin          正弦值
        sqrt         平凡根
        tan          正切值


       汇总数据

        avg         某列的平均值
        count      某列的行数
        max        某列的最大值
        min         某列的最小值
        sum        某列值之和

        select min(id) from student;  打印最小的id
        select sum(price*counts) as money from produces;统计总价

        分组数据

    select id,count(*) from student group by id;根据id分组,并且统计每组id数据数量
    select id,count(*) from student group by id having count(*)>=2;打印id组数量大于2的,having相当跟where差不多,唯一的差别在于where过滤行,having过滤组
    select id,count(*) from student where id>=2 group by id having count(*)>=2;
    group by 和 order by 的区别:
    order by 产生有序的的输出,group by 分组行,但输出可能不是分组的顺序
    select id,sum(id*age) from student group by id order by sum(id*age); 计算每个id组里面id*age的总和,再根据和排序


        子查询
    select cust_id from orders where order_num in(select order_num from orderitems where prod_id ='TNT2');
    select cust_name,cust_state,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers order by cust_name;
    select vend_name,prod_name,prod_price from vendors,products where vendors.vend_id=products.vend_id order by vend_name,prod_name

    笛卡儿积(搜索结果行的数目是地一个表的行数乘以第二个表的行数)

    select vend_name,prod_name,prod_price  from vendors,products order by vend_name,prod_name;

    内部联结
    select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id;   内部联结inner join on   联结条件用on来代替where
     
    表别名
    select a.id,a.name from student as a; 用a来代替student

    左外连接
    select customers.cust_id,orders.order_num from customers left outer join orders on customers.cust_id=orders.cust_id;  右外连接是right outer join on,其区别是左连接时,如果右边没有数据,那么只会显示左边的数据,右边显示null,右外连接则相反

    联合查询
    select vend_id,prod_id,prod_price from products where prod_price <=5   union select vend_id,prod_id,prod_price from products where vend_id in(1001,1002);把满足这两个条件的都列出来,而且会去掉重复的行,如果不想去掉重复的行,使用union all,注意union必须由两条或者两条以上的select 语句组成,且每个查询必须包含相同的列

    全文本搜索
    create table produce(note_id int auto_increment,prod_id char(10),note_date datetime,note_text text,primary key(note_id),fulltext(note_text));
    select * from produce where match(note_text) against('linux');搜索note_text中包含linux的行,刚开始的时候因为赋值给prod_id时为了加'',导致没搜索出来,后来改过来后就可以了,使用like来搜索也有差不多的结果,不过like返回的结果不是根据字符串偏前面的先返回

    布尔文本搜索
    select note_text from produce where match(note_text) against('heavy -rope*' in boolean mode);只返回匹配heavy且不包含rope开头的字符串,+代表必须存在
    select note_text from produce where match(note_text) against('heavy ropes' in boolean mode);至少包含heavy或者ropes
    select note_text from produce where match(note_text) against('"heavy ropes"' in boolean mode);包含短语heavy ropes
    select note_text from produce where match(note_text) against('>heavy <rope' in boolean mode);优先heavy 降低rope
    select note_text from produce where match(note_text) against('+heavy +(<rope)' in boolean mode);两个都必须存在,后者的优先级降低

    创建视图
    create view stuinfo as select * from student where id>2 and id<10;
    select * from stuinfo;就能查看id在2到10之间的学生信息了,而且还可以把某种固定格式的搜索结果格式转换为视图,方便使用

  • 相关阅读:
    重构项目使用Spring+Hibernate+HibernateAnnotation+GenericDao技术
    java调用bat
    Redis快速入门
    PDF中添加页面/合并 PDF 内容
    eclipse+webservice开发实例
    MYSQL Migration Toolkit 安装
    从CSDN搬家到博客园
    The server does not support version 3.0 of the J2EE Web module specification
    HibernateAnnotation入门实例
    github使用总结
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730668.html
Copyright © 2020-2023  润新知