MySQL笔记之基本查询的应用详解

 更新时间:2013年05月03日 17:49:37   作者:  
本篇文章介绍了,在mysql中基本查询的应用详解。需要的朋友参考下

参考表:student



多字段查询

复制代码 代码如下:

mysql> select id,name,birth from student;


所有字段查询
复制代码 代码如下:

mysql> select * from student;


where指定查询
复制代码 代码如下:

mysql> select * from student where id=901;
mysql> select * from student where id>=904;
mysql> select name from student where department='计算机系';


in指定集合查询
复制代码 代码如下:

mysql> select * from student where birth in(1988,1990);
mysql> select * from student where id in(903,906);

not in非范围查询
复制代码 代码如下:

mysql> select * from student where birth not in(1990,1998);


between and指定范围查询
复制代码 代码如下:

mysql> select * from student where birth between 1986 and 1988;


not between and不在指定范围的查询
复制代码 代码如下:

mysql> select * from student where id not between 904 and 906;


like字符串匹配查询
复制代码 代码如下:

mysql> select * from student where name like '_三';
mysql> select * from student where name like '张三';
mysql> select * from student where name like '张%';


not like不匹配查询
复制代码 代码如下:

mysql> select * from student where name not like '张%';


null查询
复制代码 代码如下:

mysql> select * from student where address is null;


and多条件查询
复制代码 代码如下:

mysql> select * from student where name like '张%' and birth>1985;
mysql> select * from student where name like '张%' and birth>1985 and id like '%3';


or多条件查询
复制代码 代码如下:

mysql> select * from student where id=905 or birth=1988;
mysql> select * from student where id=905 or birth=1988 or sex='女';

distinct查询结果不重复

复制代码 代码如下:

mysql> select distinct sex from student;
mysql> select distinct department from student;


order by查询结果排序
复制代码 代码如下:

mysql> select * from student order by birth;
mysql> select * from student order by birth asc;
mysql> select * from student order by birth desc;


group by分组查询
复制代码 代码如下:

mysql> select sex,group_concat(name) from student group by sex;
mysql> select sex,count(name) from student group by sex;


正则表达式查询
复制代码 代码如下:

mysql> select * from student where birth regexp '1988|1990';


limit限制查询结果数量
复制代码 代码如下:

mysql> select * from student limit 2;
mysql> select * from student limit 1,3;


相关文章

  • MySQL的自增ID(主键) 用完了的解决方法

    MySQL的自增ID(主键) 用完了的解决方法

    这篇文章主要介绍了MySQL的自增ID(主键) 用完了的解决方法,帮助大家更好的理解和学习MySQL数据库,感兴趣的朋友可以了解下
    2020-09-09
  • MYSQL必知必会读书笔记 第一章(基础)

    MYSQL必知必会读书笔记 第一章(基础)

    数据库(database)是一个以某种有组织的方式存储的数据集合。本文是小编日常收集整理些有关mysql的知识,非常具有参考价值,感兴趣的朋友一起学习吧
    2016-05-05
  • MySQL连表查询分组去重的实现示例

    MySQL连表查询分组去重的实现示例

    本文将结合实例代码,介绍MySQL连表查询分组去重,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • MySQL数据库的23个注意事项

    MySQL数据库的23个注意事项

    使用MySQL,安全问题不能不注意。以下是MySQL提示的23个注意事项
    2010-03-03
  • 分享一下Mysql常见的几个错误问题及解决方法

    分享一下Mysql常见的几个错误问题及解决方法

    这篇文章主要为大家分享一下Mysql常见的几个错误问题及解决方法,需要的朋友可以参考下
    2015-08-08
  • Mysql中substring_index函数实现字符分割一行变多行

    Mysql中substring_index函数实现字符分割一行变多行

    在MySQL中,字符串分割是一个常见的操作,本文主要介绍了Mysql中substring_index函数实现字符分割一行变多行,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Mysql 主从集群同步延迟的问题解决

    Mysql 主从集群同步延迟的问题解决

    本文主要介绍了Mysql 主从集群同步延迟的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-04-04
  • Mysql中正则表达式Regexp常见用法

    Mysql中正则表达式Regexp常见用法

    这篇文章主要介绍了Mysql中正则表达式Regexp常见用法,MySql REGEXP运算符匹配字符串,mysql正则REGEXP学习练习笔记,需要的朋友可以参考下
    2020-02-02
  • MySQL 重命名表的操作方法及注意事项

    MySQL 重命名表的操作方法及注意事项

    有时候我们会遇到重命名表的需求,比如说因业务变化,需要将表 a 重命名为表 b 。这个时候可以执行 RENAME TABLE 语句或 ALTER TABLE 语句来重命名表。本篇文章我们一起来学习下重命名表相关知识。
    2021-05-05
  • MySQL的中文UTF8乱码问题

    MySQL的中文UTF8乱码问题

    MySQL从4.x版本开始支持Unicode,3.x只有latin1编码。刚工作的时候就开始用MySQL了,用的php存取,网页xxx.php是gb2312的编码,存进去的数据用php取出来是中文,用phpMyAdmin执行select、update、dump都是中文,没有乱码问题。
    2010-05-05

最新评论