zoukankan      html  css  js  c++  java
  • Mysql学习笔记(七)查(补充)

    PS:五一还是要学习...虽然有点苦逼..但是路是自己选的,那么自己就要坚持的走下去...

    学习内容:

    1.数据库查找的补充...

    查找涉及的东西比较多,在上一个章节没有完全介绍...我们还是以pet表格为例,在这里进行补充....

    模式匹配:

      所谓模式匹配,无非就是按照某种模式进行查找...我们给出的一个模范,然后按照这个模范进行匹配完成的查找就属于模式匹配查找...模式匹配的查找中,我们不能够使用=或!=来操作,应该使用like或not like 进行操作...

    //查找名字以b开头的宠物...
    select * from pet where name like 'b%';
    //查找名字以fy结尾的宠物..
    select * from pet where name like '%fy';
    查找名字里包含w的宠物...
    select * from pet where name like '%w%';

    这类属于的是sql标准的模式匹配..还有另一种类型的匹配属于扩展正则表达式的模式匹配...下面进行简单介绍..

    扩展正则表达式的一些字符..

    • '.'匹配任何单个字符..
    • [...]匹配在方括号包括的字符,这个可以是指定值,也可以是一个范围值。。[abc]匹配字符只能有'a','b','c'为命名范围的字符..[a-z]匹配a-z之间为命名范围的字符..
    • '*'匹配0个或者多个*之前的字符..

    注意:

    • 如果regexp模式与被测值的任何地方匹配,那么该模式就匹配。。
    • 为了定位一个模式以便它必须匹配测试值的开头或者结尾...该模式开头使用'^',或者结尾使用'$'。。
    //查找所有以b开头的宠物名字...不区分大小写
    
    select * from pet where name regexp '^b';
    
    //区分大小写...
    
    select * from pet where name regexp BINARY '^b';
    
    //查找以fy结尾的宠物名字...
    
    select * from pet where name regexp 'fy$';

    计数行:就是使用count(*)函数..

    select count(*) from pet;//显示数据库数据的行数..

    当我们想要查找一些特定的数据信息的时候..可以使用count(*)函数,配合着group by来进行查询..比如说,我们想要查询宠物猫和狗的数据信息的时候..我们没必要查询整个表,只需要使用count函数配合group by就可以查询的到..

    select species,sex count(*) from pet where species="dog" or species="cat"group by species,sex;

    常用的一些查询的例子:

    比如说,我们建立了一个商店的数据库。。。商店里有一些物品,并且每件物品都有一个价值...

    mysql>create table shop
    ->(
    ->       article int(4) unsigned zerofill default '0000' not null,
    ->       dealer char(20) default '' not null,
    ->       price double(16,2) default '0.00' not null,
    ->       primary key(article,dealer)
    ->);
    
    mysql>insert into shop values(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C','1.69'),(3,'D',1.25),(4,'D',19.95);
    
    1.查询列的最大值...
    select max(article) as article from shop;
    
    2.查询某个列的最大值的一行数据...两种方法..
    select article,dealer,price from shop where price=(select max(article) from shop);
    select article,dealer,price from shop order by price desc limit;
    
    3.列的最大值,与第一个不一样的地方在于,第一个是显示所有物品里的最大值,这种属于显示每一种物品的一个最大值..
    select article max(price) as price from shop order by article;
    
    4.拥有某个字段的组间最大值的行..
    select article,dealer,price from shop s1 where price=(select max(s2.price) from shop s2 where s1.article=s2.article);

    第四条使用临时表的形式也可以做到...

    CREATE TEMPORARY TABLE tmp 
    (
      article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
      price  DOUBLE(16,2)       DEFAULT '0.00' NOT NULL
    );
      
      LOCK TABLES article read;
      
      INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;
      
      SELECT article, dealer, price FROM shop, tmp
      WHERE shop.article=tmp.articel AND shop.price=tmp.price;
      
      UNLOCK TABLES;
      
      DROP TABLE tmp;

    这个是看别人写的,自己并没有完全弄懂..有兴趣的可以研究研究...还有一种方式:

    SELECT article,SUBSTRING( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 7) AS dealer, 0.00+LEFT(MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 6) AS price FROM shop
    GROUP BY article;

    使用用户变量:

    如果我们想要找出价格最高和最低的物品..并且还不想保存到客户端的临时变量中时..我们可以使用用户变量...

    mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;
    mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;

    查询还有使用外键查询。。使用auto_increment查询...自己只是了解,就不在这里班门弄斧了...

     多表查询:

    简单的介绍一下多表查询。。。。多表查询涉及的内容很多..在这里只是简单的介绍一下使用select子语句进行查询..

    CREATE TABLE IF NOT EXISTS contact(  
    contact_id int(11) NOT NULL AUTO_INCREMENT,  
    user_name varchar(255),  
    nom varchar(255),  
    prenom varchar(255),  
    mail varchar(64),  
    passcode char(64),  
    PRIMARY KEY(contact_id)  
    );  
    CREATE TABLE IF NOT EXISTS droit(  
    droit_id int( 11 ) NOT NULL AUTO_INCREMENT ,  
    droit varchar(255),  
    PRIMARY KEY(droit_id)  
    );  
    CREATE TABLE IF NOT EXISTS contactdroit(  
    contactdroit_id int(11) NOT NULL AUTO_INCREMENT,  
    contact_id int( 11 ),  
    droit_id int( 11 ),  
    PRIMARY KEY( contactdroit_id )  
    );  
    Insert into contact(contact_id, user_name) values(1,'user1');  
    Insert into contact(contact_id, user_name) values(2,'user2');  
    Insert into contact(contact_id, user_name) values(3,'user3');  
    Insert into droit(droit_id, droit) values(1,'admin');  
    Insert into droit(droit_id, droit) values(2,'superuser');  
    Insert into contactdroit(contact_id, droit_id) values(1, 1);  
    Insert into contactdroit(contact_id, droit_id) values(2, 1);  
    Insert into contactdroit(contact_id, droit_id) values(3, 2);  
     
    SELECT c.contact_id, d.droit_id, d.droit FROM contact c, contactdroit cd, droit d   
    where c.contact_id = cd.contact_id  
    and cd.droit_id = d.droit_id;  

    想了解更多的多表查询:http://blog.sina.com.cn/s/blog_6ad62438010168lg.html

  • 相关阅读:
    shell script数组使用函数输出
    Yii2文件上传
    ubuntu 安装遇到黑屏
    使用函数
    ionCube 安装
    记录LNMP环境彻底删除绑定域名及网站文件夹/文件的过程
    lnmp环境 开启pathinfo
    国外知名设计教程网址收集
    26个国外在线教育网站
    前端学习网站汇总
  • 原文地址:https://www.cnblogs.com/RGogoing/p/4470581.html
Copyright © 2011-2022 走看看