sql查询的数据出现乱码问题

sql查询的数据出现乱码问题,第1张

1。select from v$nls_parameters

查询nls的参数,获得数据库服务器端的字符编码

NLS_LANGUAGE

NLS_CHARACTERSET

2。修改本地环境变量,设置

NLS_LANG = SIMPLIFIED CHINESEZHS16GBK //这个是我们的数据库字符编码

NLS_LANG格式:

NLS_LANG = language_territorycharset

有三个组成部分(语言、地域和字符集),每个成分控制了NLS子集的特性。其中:language 指定服务器消息的语言。

territory 指定服务器的日期和数字格式。

charset 指定字符集

in *** 作符允许我们在 WHERE 子句中规定多个值。

where 字段名 in (值1,值2,值3)

示例: where age in (20,21,25);

age 字段的值 只要是 20或者21或者25 的数据 都满足条件

where not in (值1,值2,值3)</pre>

示例: where age in (20,21,25);

age 字段的值 只要 不是 20或者21或者25 的数据 都满足条件

select

from sc

where score in (70,80,90);

select

from sc

where score not in (70,80,90);

select

from student

where birthday in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06');</pre>

select

from student

where birthday not in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06');

MySQL中的分页查询, limit 通常放在sql 语句的最末尾

limit 4

查询前4条数据

limit 4,3

从第4条数据之后开始,查询3条数据

也就是 第5,6,7条的数据

limit 4 offset 10;

offset后面的数字是指记录数

从第10条 之后的数据开始 查询4条数据

也就是 第 11,12,13,14条数据

select from student limit 5;

select from student limit 9,6;

select from student limit 6 offset 9;

查询成绩表中 前7条的数据;

select from sc limit 7;

查询成绩表中 第 5到10 条的数据

select from sc limit 4,6;

查询成绩表中 第 3到8 条的数据;

select from sc limit 2 , 6;

如果字段没有要求非空,则字段值 可以为空,就是null值。如果要查询某个 字段 为 null的数据,不能使用 字段名=null,要用 字段名 is null;

where 字段名 is null;

字段名 为空的符合条件

where 字段名 is not null;

字段名 不为空的符合条件

1查询teacher表,给tid 起别名为 教师编号,tname 起别名为 教师姓名

select tid as 教师编号,tname as 教师姓名 from teacher

2查询 sc 表, 给 cid 起别名为 课程编号,sid 起别名为 学生编号, score起别名为分数;

select cid 课程编号 ,sid 学生编号,score 分数 from sc

3查询 course 表,给表起别名为 c,使用 c字段查询 表数据

select c from course as c ;

起别名是为了后续学习多表联查 做铺垫,语法十分简单

比如查询 student 表的 学生姓名 和 生日 的sql语句:

select sname,birthday from student;

给 sname,birthday字段 起别名 ,改为中文

select sname as 姓名,birthday as 生日 from student;

起了别名后,查出来的字段会以别名展示。

as 关键字也可以省略,字段名 和 别名之间 用空格隔开即可

select sname 姓名, birthday 生日 from student;

例子:

select asname, abirthday from student as a;

给表 起别名 同样是 使用 as关键字(可以使用空格隔开代替as关键字), a 就代表 student表,查询字段时,可以 使用 asname 代表 student的sname字段。

给表起别名 通常用在 多表查询,本次使用演示,先熟悉语法。

在某些复杂的场景下,可能需要多条sql 才能查询出想要的数据,把多条sql拼接起来 就是嵌套查询

句式1

select XXX from A表 where xx =/in (select XXX from B表)

句式2

select XXX from (select XXX from XXXX)b

句式3

select XXX from A表 a

join (select XXX from B表)b on axx = bxx

select from sc where sid = (select sid from student where sname = '周梅');

select from sc where sid in (select sid from student where sex='女');

select asname,bscore,cscore from student a

inner join (select sid,score from sc where cid =1) b on asid = bsid

inner join (select sid,score from sc where cid = 2) c on asid = csid

where bscore › cscore;

聚合函数对一组值执行计算,并返回单个值,也被称为组函数。

就是sql提供的一种查询方法,比如查询 最大值,最小值,总数,平均等等。

聚合函数不能写在where的查询条件里面

查询 最小值

select min(字段) from 表;

查询 最大值

select max(字段) from 表;

select max(tid) from teacher;

select min(tid) from teacher;

select max(ascore) ,bcname from sc a

inner join course b on acid=bcid

where asid =(select sid from student where sname='周梅') group by bcname limit 1 ;

查询 该字段的 总数

select sum(字段) from 表

查询 该字段的 平均数

select avg(字段) from 表

select avg(score) from sc;

select avg(sscore) from student a inner join sc s on asid = ssid

where asex ='女';

查询 数据 总条数

count( ) 是查询总共有多少条数据

count(字段) 是查询该字段 不为 null的 总共有多少条数据

例:

select count( ) from 表

去重,查询数据时,相同的数据只展示 一条

语法:

select distinct 字段 from 表;

例:去重查询学生表中的生日字段

select distinct birthday from student;

1、数据库增加数据:

1)插入单行

insert [into] <表名> (列名) values (列值)

例:insert into t_table (name,sex,birthday) values ('开心朋朋','男','1980/6/15')

2)将现有表数据添加到一个已有表 insert into <已有的新表> (列名) select <原表列名> from <原表名>

例:insert into t_table ('姓名','地址','电子邮件')

select name,address,email from t_table

3)直接拿现有表数据创建一个新表并填充 select <新建表列名> into <新建表名> from <源表名>例:select name,address,email into t_table from strde

2、数据库删除数据:

1)删除<满足条件的>行

delete from <表名> [where <删除条件>]。

例:delete from t_table where name='开心朋朋'(删除表t_table中列值为开心朋朋的行)

2)删除整个表 truncate table <表名>

truncate table tongxunlu

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表

3、数据库修改数据 update <表名> set <列名=更新值> [where <更新条件>]

例:update t_table set age=18 where name='蓝色小名'

4、数据库查询数据:

1)精确(条件)查询

select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]

2)查询所有数据行和列。例:select from a

说明:查询a表中所有行和列

3)使用like进行模糊查询

注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用

例:select from a where name like '赵%'

说明:查询显示表a中,name字段第一个字为赵的记录

4)使用between在某个范围内进行查询

例:select from a where nianling between 18 and 20

说明:查询显示表a中nianling在18到20之间的记录

5)使用in在列举值内进行查询

例:select name from a where address in ('北京','上海','唐山')

说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段

扩展资料:

插入之前需要创建数据表,创建方式如下:

CREATE TABLE 表名称

(

列名称1 数据类型,

列名称2 数据类型,

列名称3 数据类型,

)

例如:--流程步骤定义表

create table T_flow_step_def(

Step_no    int not null,     --流程步骤ID

Step_name    varchar(30)    not null, --流程步骤名称

Step_des    varchar(64)    not null,    --流程步骤描述

Limit_time    int not null,     --时限

URL     varchar(64)    not null,     --二级菜单链接

Remark    varchar(256)    not null,

)

参考资料:

百度百科-sql语句大全

以上就是关于sql查询的数据出现乱码问题全部的内容,包括:sql查询的数据出现乱码问题、数据库表查询进阶(1)、数据库的增删改查等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10219738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存