DQL query data in the table

DQL query data in the table: the query (most complex statement) does not modify the data in the database, just a way of displaying data

Syntax:

select 
	field list 
from 
	list of table names 
where 
	condition list 
group by 
	grouping field 
having 
	conditions after the packet 
order by 
	sorting 
limit 
	tab defined

First, the underlying query

  1, look-up table all the rows and columns of data, the use of * indicates all columns

select * from 表名;

  2, query specifies column

select a name field, 2 field names, field names 3, ... from the table name;

  3, column alias specified query

    The benefits of using aliases: When displayed with the new name, does not modify the structure of the table

    ① alias column

1 AS SELECT field name aliases, the alias field name 2 AS ... FROM table name;

    ② specify the column and table aliases

1 AS SELECT field name aliases, the alias field name 2 ... FROM AS AS table alias table;

    The reason for using aliases table for multi-table queries.

     Note : as keywords can be omitted, the omission can be separated by a space.

  3, eliminating duplicates

     The query specifies column and the results of non-duplication of data

SELECT DISTINCT Field Name FROM table;

  4, involved in computing query results

    A column data ① and the fixed value calculation

 

SELECT 1 plus fixed column name FROM table;

 

    ② a column of data and other data columns involved in computing

 

Column 1 + Column name SELECT 2 FROM table name;

 

           Note :

      •  Involved in operations must be numeric type
      •     如果两列中有一列为null,null参与的运算,计算结果为null,所以需要用到 ifnull()函数。
      •     ifnull(表达式1,表达式2) 表达式1:哪个字段需要判断是否为 null;表达式2:如果该字段为 null 后的替换值。

     Demo:

select math+ifnull(english,0) as 总成绩 from student;

  

二、条件查询

  条件查询是对记录指定查询条件,对记录进行过滤。

  语法格式

SELECT 字段名 FROM 表名 WHERE 条件;

  1、使用比较运算符

    比较运算符:>、<、<=、>=、=、<> (<>在 SQL 中表示不等于,在MySQL中可以使用 != 来表示)

    Demo:

select * from student3 where age <> 20;
select * from student3 where age != 20;      // 查询 age 不等于20岁的学生

  

  2、使用范围查询

    关键字:between... and...(包含边界值)

    Demo:

select * from student where age between 20 and 23;
select * from student where ange >= 20 and age<=23;    // 两者相等

  

  3、使用 in(集合)

    in 里面的每个数据都会作为一次条件,只要满足条件的就会显示。

SELECT 字段名 FROM 表名 WHERE 字段 in (数据 1, 数据 2...);

    Demo:

select * from student where age [not] in(18,20,23);     // not 为可选值

  4、使用模糊查询(Like 关键字)

    Like 表示模糊查询,其中有两个通配符:"%" 匹配任意多个字符串;"_" 匹配任意一个字符。

SELECT * FROM 表名 WHERE 字段名 LIKE '通配符字符串';

    Demo :

select * from student where name like '%枫%';        // 查询姓名中包含"枫"字的学生

  

  5、使用逻辑运算符

    逻辑运算符

    

逻辑运算符 说明
and 或 && 与,SQL中建议使用前者,后者并不通用
or 或 || 或,
not 或 !

     Demo:

select * from student where age>18 or sex='男';

  6、查询某一列为 Null 的值

     Demo :

SELECT * FROM student WHERE english IS (NOT) NULL;    // 判断英语成绩不为 null的学生

    注意:对 null 的值不能使用 = (!=)进行判断。

三、排序查询

四、分组查询

五、分页查询

     limit 是限制的意思,所以 limit 的作用就是限制查询记录的条数。

   语法格式

SELECT *|字段列表 [as 别名] FROM 表名 [WHERE 子句] [GROUP BY 子句][HAVING 子句][ORDER BY 子句][LIMIT 子句];

    LIMIT 语法格式:

LIMIT offset,length;

     offset:起始行数,从 0 开始计数,如果省略,默认就是0

   length:返回的行数

   Demo:

select * from student3 limit 10,5;

    注意

    ① 如果第一个参数是 0 可以省略写

    ② 如果最后一页不够显示的条数,有多少显示多少

    ③ 显示记录公式:开始的索引 = (当前的页码 - 1)* 每页显示的条数

    ④ limit 是一个“MySQL”的方言(只适用于MySQL中)

六、DOS 命令窗口操作数据乱码问题的解决

  1、使用 DOS 命令进行 SQL 语句操作可能会出现中文乱码现象

    

  2、乱码产生的原因

    

  3、查看 MySQL 内部设置的编码

    查看包含 character 开头的全局变量

show variables like 'character%';

     执行效果:

              

  4、解决方法

    修改 client、connection、results 的编码为 GBK,保证和 DOS 命令行编码保存一致。

单独设置 说明
set character_set_client=gbk; 修改客户端的字符集为 GBK
set character_set_connection=gbk; 修改连接的字符集为 GBK
set character_set_results=gbk; 修改查询的结果字符集为 GBK

     同时设置三项

set names gbk;

    注意退出 DOS 命令行就失效了,需要每次都配置

            

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11575447.html