6、连接运算符
sql:SELECT name 姓名,age 年龄,name||' IS '||age||' age' AS "用户年龄" FROM userInfor ;
说明:连接运算符是双竖线“||”。通过连接运算可以将两个字符串连接在一起。
7、使用的关键字DISTINCT消除重复显示
sql:SELECT DISTINCT sex FROM userInfor ;
说明:关键字DISTINCT消除重复显示
8、查询结果的排序
升序:asc
sql:SELECT * FROM userInfor ORDER BY sexASC;
降序:desc
sql:SELECT * FROM userInfor ORDER BY sexDESC;
多字段排序:
sql:SELECT * FROM userInfor ORDER BY sex desc , birthday asc;
说明:ORDER BY 从句出现在SELECT语句的最后,后跟要排序的列。ASC表示升序排序,DESC表示降序排序。默认的排序顺序为升序。可以按多列进行排序,先按第一列,然后按第二列、第三列......。
9、简单条件查询
运算符
功 能
实 例
>,<
大于,小于
Select * from userInfor where age>10
>=.<=
大于等于,小于等于
Select * from userInfor where age>=10
=
等于
Select * from userInfor where age=10
!=,<>,^=
不等于
Select * fromuserInforwhere age!>=12
sql:SELECT * FROM userInfor where age>10;
说明:在FROM从句后使用WHERE从句,在WHERE从句中给出限定的条件,因为限定条件是一个表达式,所以称为条件表达式。条件表达式中可以包含比较运算,表达式的值为真的记录将被显示。字符串和日期型数据的值是包含在单引号中的。字符的值对大小写敏感
10、复合条件查询(NOT,AND,OR)
运算符
说 明
实 例
AND
逻辑与,表示两个条件必须同时满足
Select t.* from userInfor t where t.age>2 and t.birthday=to_date('1986-02-08','yyyy:MM:dd');
OR
逻辑或,表示两个条件中有一个条件满足即可
Select t.* from userInfor t where t.age>2 and t.birthday=to_date('1986-02-08','yyyy:MM:dd');
NOT
逻辑非,返回与某条件相反的结果
Select t.* from userInfor t where not t.birthday=to_date('1986-02-08','yyyy:MM:dd');
sql:Select t.* from userInfort where t.age>2 and not t.birthday=to_date('1986-02-08','yyyy:MM:dd'); 说明:运算的优先顺序是NOT,AND,OR。如果要改变优先顺序,可以使用括号。
11、特殊运算符
运 算
功 能
实 例
[NOT]
BETWEEN…AND…
用于测试是否在范围内
Select * from userInforWhere age between 10
and 12
Select name 用户,age from userInfor where exists(select age from userInfor where birthday>to_date('1986-02-08','yyyy:MM:dd'));
说明:1、BETWEEN:对于数值型或日期型数据,表示范围时用。下限在前,上限在后,不能颠倒。查询范围中包含上下限的值
2、LIKE:完成按通配符查找字符串的查询操作,该操作符适合于对数据进行模糊查询;%:代表0个或多个任意字符。_ :代表一个任意字符。 问题1:any , some ,all 区别 解答:
* Some:表示满足其中一个的意义,是用or串起来的比较从句。
sql: Select t.* from userInfor t Where t.age=some(select age from userInfor where sex=0);
理解:先执行:select age from userInfor where sex=0
再执行:select t.* from userInfor t Where t.age=26ort.age=12; * Any:也表示满足其中一个的意义,也是用or串起来的比较从句,区别是any一般用在非“=”的比较关系中,这也很好理解,英文中的否定句中使用any肯定句中使用some,这一点是一样的。
sql: Select t.* from userInfor t Where t.age<any(select age from userInfor where sex=0);
理解:先执行:select age from userInfor where sex=0
再执行:select t.* from userInfor t Where t.age<26ort.age<12; * All:表示满足其中所有的查询结果的含义,使用and串起来的比较从句。
sql:select t.* from userInfor t Where t.age<all(select age from userInfor where sex=0);
理解:先执行:select age fromuserInfor where sex=0
再执行:selectt.* from userInfor t Where t.age<26 and t.age<12; 问题二:in , EXISTS的理解 解答: *EXISTS
sql:Select name 用户,age from userInfor where exists(select age from userInfor where birthday>to_date('1986-02-08','yyyy:MM:dd'));