常用的sql函数
1、isnull (expr) mysql中和oracle中不一样、具体用到的时候再根据需求选取。参考资料上网查(在mysql中、若表达式不为空则返回1、为空返回0)ifnull(expr,0) 是否存在、存在返回存在的实际值、不存在返回0;oracle中相似功能的函数、nvl(expression , value)——判断expression的值是否为空、为空则返回后面的value、不为空则返回expression的值;oracle中判断null —— where xxx is null、myslq中 where xxx != null(不会报错、但是执行结果异常)。同样使用 where xxx is null
2、注意group by 的使用、对分组后加条件用 having 有时候加group by 查询的结果反而比不加的多、比如返回结果中有avg这样的函数的时候、group by 后面的分组条件使用多个的时候要用,分开、不是and 否则会没有查询结果!、
3、要想显示一张表中所有的结果可以使用left join 或者right join exp:select * from student t1 left joinscore t1 on t1.sno = t2.sno
4、直接从两张表中查询数据的时候,select * from studentt1 , sc t2 ;不加条件就是求两张表的笛卡尔积、即结果是两张表的行数相乘、加上条件后、只取非空的数据、空数据要是想娶的话要用left join 或者 right join、、、、
5、% 的三种写法 1、a% 匹配a开头、2、%a% 匹配包含a 3、%匹配以a结尾
6、select * from teacher t where t.tname like N’陈%’; 中N的意义:MS-SQL Serverselect * from Bookwhere BookName like'%C语言%' 在SQL2000下能正常找到,在2005下不能,因为语句中的中文字体, 但是使用select * from Book whereBookName like N'%C语言%' ,这样就完合正常了, Like后的N是表示什么意思呢unicode字符 N转换字符串为nchar,nvarchar 当把客户端发送的非 Unicode 数据以 Unicode 存储在服务器中时,如果具备下列条件之一,则来自任何客户端的任何代码页的数据都可以正确地存储字符串常量以大写字母 N 开头。无论客户端应用程序是否能够识别 Unicode,必需这样做。如果没有字母 N 前缀,则 SQL Server 会将字符串转换为与数据库的默认排序规则相对应的代码页。此代码页中没有的字符都将丢失。
7、函数 left(<characterexpression>,<integer_expression> )返回 character expression 左起、integer_expression个字符。mysql中可用
函数:charindex(<’substring_expression’>, <expression>) 其中substring_expression是要寻找的表达式、expression可为字符串也可以是列名表达式、如果没有子串则返回0值。mysql中不可用。
8、从N张表中查询结果时、将多张表连接起来必定至少有N-1个条件、如果再对查询结果进行筛选、则需要额外加筛选条件(也可以理清关系后用嵌套查询来查、不过这是一种很土的方法、以后项目中尝试着用这种方法来查、土方法如果碰到特殊的就比较难处理了、比如内外连接什么的)
9、留个疑问:select t1.* from table1 t1 , table2 t2 where t1.sno = t2.sno 和
select t1.* from table1 t1 inner join table t2 on t1.sno = t2.sno有什么区别?
10、 cross join 将两个表进行交叉连接、即结果为两个表的笛卡尔积、其实select * from a cross join b 的结果和 select * from a , b 效果是一样的、cross join 装逼用的!
11、 “查询没有学全所有课程的同学的信息”这条查询语句中为什么用左连接而不是用内连接或者交叉连接、或者自然连接?答案很简单、要查询没有学全所有课程的同学、当然包括一门课程也没有学的同学、如果用自然连接、那么只有两张表中有相同数据的时候才会被显示、这样没有选择任何课程的同学的信息就会丢失、而改成左连接、让Student表中所有数据都被查出来则符合要求!!! 、、
12、 charindex(expression ,condition) | instr (condition , expression)
13、mysql中截取字符串、
-- 截取从str的pos下标开始到str最后、pos从1开始、包括pos位置的字符、str可以是expression。SUBSTR(str,pos)
SUBSTR(str FROM pos)
SUBSTRING(str,pos)
SUBSTRING(str FROM pos)
-- 截取str从pos下标开始、len个字符、pos从1开始、包括pos下标的字符
SUBSTR(str,pos,len)
SUBSTR(str FROM pos FOR len)
SUBSTRING(str,pos,len)
SUBSTRING(str FROM pos FOR len)
14、 extract (year from t.age)-------the slq type of age is date !
15、 查询结果的集合运算:union —— 对两个结果集求并集、不包括重复行
union all —— 对两个结果集求并集、包括重复行
intersect —— 对两个结果集求交集、不包括重复行
minus —— 对两个结果集求差集、不包括重复行
16、 修改表名、添加、删除列、修改列名、类型
create table test_student (
sno varchar2(22) primary key ,
sname varchar2(22) not null,
ssex char (1) not null check (ssex in ('男','女')),
sage number not null check ( sage > 14 ),
sdept varchar2(222)
)
--修改表名
alter table test_student rename to new_test_student
--添加表列
alter table test_student add sidentify varchar2(55) not null
--修改列名
alter table test_student rename column sidentify to new_sidentify
--修改字段类型
alter table test_student modify sidentify number null
--删除表列
alter table test_student drop column sidentify
17、oracle查询当前用户下所有表名
select table_name from user_tables;
18、存储过程声明变量的类型:
1)与表的某个字段相同: n_id t_student.id%type;
2)定义Cursor时:cursor cur is select * from user_tables; tableInfo user_tables%rowtype;
3)用于存放cursor中数据的变量 variablecur%rowtype;
19、根据现有的表创建一个新的表、新表与原有表中数据相同
create table emp1 as select * from emp;
20、在mysql中、实现添加一条记录时、有一个字段自动记录当前系统时间、在创建表时指定记录时间的列的类型为timestamp 、给他一个默认值:
current_timestamp;
exp:
create table student (
id int not null primary key auto_increment,
sno int not null ,
sname varchar(22) not null,
age int ,
addtime timestamp default current_timestamp
);
这样之后、每次向这张表中插入一条学生记录、此记录都会有添加时间。
21、mysql中的分页limit详解(优化在另一篇转载):
1)select * from student limit 5 , 10 //检索记录行 6-15、即从第六条开始检索、取剩下的10条、
2)select * from student limit 10 , -1 //检索记录行 11 到最后、
3)select * from student limit 5 //检索前五个记录行、等价于 : select * from student limit 0 , 5
22、mysql中date的格式方式:
mysql中DATE_FORMAT(date, format)函数可根据format字符串格式化日期或日期和时间值date,返回结果串。
也可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值,以便得到所希望的格式。根据format字符串格式化date值:
下面是函数的参数说明:
%S, %s 两位数字形式的秒( 00,01, . . ., 59)
%i 两位数字形式的分( 00,01, . . ., 59)
%H 两位数字形式的小时,24 小时(00,01, . . ., 23)
%h, %I 两位数字形式的小时,12 小时(01,02, . . ., 12)
%k 数字形式的小时,24 小时(0,1, . . ., 23)
%l 数字形式的小时,12 小时(1, 2, . . ., 12)
%T 24 小时的时间形式(hh : mm : s s)
%r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)
%p AM 或P M
%W 一周中每一天的名称( Sunday, Monday, . . ., Saturday)
%a 一周中每一天名称的缩写( Sun, Mon, . . ., Sat)
%d 两位数字表示月中的天数( 00, 01, . . ., 31)
%e 数字形式表示月中的天数( 1, 2, . . ., 31)
%D 英文后缀表示月中的天数( 1st, 2nd, 3rd, . . .)
%w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, . . ., 6=Saturday)
%j 以三位数字表示年中的天数( 001, 002, . . ., 366)
% U 周(0, 1, 52),其中Sunday 为周中的第一天
%u 周(0, 1, 52),其中Monday 为周中的第一天
%M 月名(January, February, . . ., December)
%b 缩写的月名( January, February, . . ., December)
%m 两位数字表示的月份( 01, 02, . . ., 12)
%c 数字表示的月份( 1, 2, . . ., 12)
%Y 四位数字表示的年份
%y 两位数字表示的年份
%% 直接值“%”
示例:
select date_format(日期字段,’%Y-%m-%d’) as ‘日期’ from test
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');
-> 'Saturday October 1997'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');
-> '22:23:00'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00',
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> SELECT DATE_FORMAT('1999-01-01', '%X %V');
-> '1998 52'