增
删
改
查
|| 连接符的使用
SELECT
T .org_id,
org_name,
T .org_id || T .org_name NAME
FROM
TSYS_ORGANIZATION T
通配符
SQL 通配符必须与 LIKE 运算符一起使用。
% 替代一个或多个字符
"Persons" 表中选取居住在包含 "lond" 的城市里的人:
样例:SELECT * FROM Persons WHERE City LIKE '%lond%'
_ 仅替代一个字符
样例: "Persons" 表中选取的这条记录的姓氏以 "C" 开头,然后是一个任意字符,然后是 "r",然后是任意字符,然后是 "er":
SELECT * FROM Persons WHERE LastName LIKE 'C_r_er'
[charlist] 字符列中的任何单一字符
"Persons" 表中选取居住的城市以 "A" 或 "L" 或 "N" 开头的人:
样例:SELECT * FROM Persons WHERE City LIKE '[ALN]%'
[^charlist]& [!charlist] 不在字符列中的任何单一字符
"Persons" 表中选取居住的城市不以 "A" 或 "L" 或 "N" 开头的人:
样例:SELECT * FROM Persons WHERE City LIKE '[!ALN]%'
case when 函数
--简单Case函数
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函数
CASE
WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END
SELECT
(
CASE
WHEN t1.iscashvirtual = '1' THEN
'现金'
WHEN t1.isbillvirtual = '1' THEN
'票据'
ELSE
CASE
WHEN t1.deposittype = '1' THEN
'活期'
WHEN t1.deposittype = '2' THEN
'定期'
WHEN t1.deposittype = '3' THEN
'保证金'
WHEN t1.deposittype = '4' THEN
'通知'
ELSE
'其它'
END
END
) AS deposittype
FROM
t_ba_accounts t1
SELECT Company, OrderNumber FROM Orders ORDER BY Company
以字母顺序显示公司名称:
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
以字母顺序显示公司名称(Company),并以数字顺序显示顺序号(OrderNumber):
默认升序 asc
降序排序desc
**GROUP BY **
group by 分组 一般用于统计
select
accountname,
count(accountnumber)
from t_ba_accounts GROUP BY accountname
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。
聚合函数是比较where、having 的关键。
where>聚合函数(sum,min,max,avg,count)>having
若须引入聚合函数来对group by 结果进行过滤 则只能用having。
样例:select sum(score) from student where sex='man' group by name having sum(score)>210
注意:
1、where 后不能跟聚合函数,因为where执行顺序大于聚合函数。
2、where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。
3、having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组
--举例子说明:查询table表查询每一个班级中年龄大于20,性别为男的人数
select COUNT(*)as '>20岁人数',classid from Table1 where sex='男' group by classid,age having age>20
--需要注意说明:当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:
--执行where子句查找符合条件的数据;
--使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;最后用having 子句去掉不符合条件的组。
--having 子句中的每一个元素也必须出现在select列表中。有些数据库例外,如oracle.
--having子句和where子句都可以用来设定限制条件以使查询结果满足一定的条件限制。
--having子句限制的是组,而不是行。where子句中不能使用聚集函数,而having子句中可以。
SELECT
t2.org_code || '-' || t2.org_name orgname,
t3.code || '-' || t3. NAME bankname,
t4.code || '-' || t4. NAME banklocationname,
t1.accountnumber accountnumber,
t1.accountname accountname,
t1.openeddate,
t6.code || '-' || t6. NAME accounttype,
(
CASE
WHEN t1.iscashvirtual = '1' THEN
'现金'
WHEN t1.isbillvirtual = '1' THEN
'票据'
ELSE
CASE
WHEN t1.deposittype = '1' THEN
'活期'
WHEN t1.deposittype = '2' THEN
'定期'
WHEN t1.deposittype = '3' THEN
'保证金'
WHEN t1.deposittype = '4' THEN
'通知'
ELSE
'其它'
END
END
) AS deposittype,
t5.code || '-' || t5. NAME currency,
(
SELECT
T .c_caption
FROM
tdictionary T
WHERE
T .l_keyno = '1025'
AND T .c_keyvalue = t1.accountstate
AND T .c_caption != '#'
) AS accountstatus
FROM
t_ba_accounts t1,
tsys_organization t2,
t_sy_banks t3,
t_sy_banklocations t4,
t_bd_currencies t5,
T_BD_CATEGORIES t6
WHERE t1.orgid = t2.org_id
AND t1.bankid = t3.urid
AND t1.banklocationid = t4.urid
AND t1.currencyid = t5.urid
AND t1.accounttypeid = t6.urid
-- ${if(len(bankname) == 0,""," and t3.urid in ('" + bankname + "')")}
-- and t2.org_id in ('${orgname}')
-- ${if(len(org_name) == 0,""," and t2.org_name in ('" + org_name + "')")}
-- and t2.org_id in ('${orgname}')
-- ${if(len(currencyid) == 0,""," and t5.urid in ('" + currencyid + "')")}
-- ${if(len(openeddatestart) == 0,""," and t1.openeddate >= to_date('" + openeddatestart+"','yyyy-MM-dd')")}
-- ${if(len(openeddateend) == 0,""," and t1.openeddate <= to_date('" + openeddateend+"','yyyy-MM-dd')")}
--${if(len(accountstatus) == 0,""," and t1.accountstate in ('" + accountstatus + "')")}
ORDER BY
t2.org_code,
t6.code,
t3.code,
t1.accountnumber ASC