SQL常用函数及sql句型

POATGRESQL 拥有很多可用于计数和计算的内建函数。
函数总体语法
SELECT function(列) FROM 表

COUNT():用于计算一个数据库表中的行数(记录总数)。
MAX():允许我们选择某列最大值(最高)。
MIN():允许我们选择某列最小值(最低)。
AVG():选择若干表列的平均值。
SUM():允许为一个数值列共选择。
ARRAY():数组聚合函数把输入值,包括空值,串连成一个数组。
数字函数:PostgreSQL的函数需要在SQL来操作数字的完整列表。
字符函数:PostgreSQL函数在PostgreSQL需要对字符串进行操作的完整列表。

这里附上自己写着写着就忘了的基础sql句语法。
除了查询,其他操作都需要数据库提交(commit)
查询: select 需要的结果 from 表名 限定条件
写法:SELECT… FROM… join_type JOIN…ON…WHERE …GROUP BY …HAVING …ORDER BY … limit
执行顺序:
FROM…ON…OUTER(join)…WHERE…GROUP BY…HAVING…SELECT…DISTINCT…ORDER BY…LIMIT…这个执行过程中产生了很多的虚表,最终只显示了最后一张虚表。

删除:delete from 表名 where 限定条件(这里特别说明,删除条件十分重要)

更新:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

插入:INSERT INTO table_name (列1, 列2,…) VALUES (值1, 值2,…) 插入满行,可以不写列名

增加一列:ALTER TABLE table_name ADD column_name datatype

删除表中一列:ALTER TABLE table_name DROP COLUMN column_name

修改某一列: ALTER TABLE table_name ALTER COLUMN column_name datatype

UNION 操作符:用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。ALL: 可选,返回所有结果集,包含重复数据。

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

GROUP BY 语句根据一个或多个列对结果集进行分组。在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。一般跟名。

SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

JOIN
可以在 SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。
JOIN 按照功能大致分为如下三类:
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
在这里插入图片描述
LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
在这里插入图片描述
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
在这里插入图片描述

update/delete的联表操作:

#在postgresql,oracle中(曾经以为更新每次只更新一个,但是这样的更新是将表里面符合条件都更新过去,并且可多个值),尴尬的是有次在mysql中用了,发现
update student set age=u.age,sex=u.gender from user u where student.id=u.id;
#以下是mysql写法,也适用于postgresql,oracle
delete T1 from table1 T1, table2 T2 where T1.a = T2.a // 删除表1中与表2相同a
update table1 T1, table2 T2 set T1.a = T.b where T1.a = T2.a
注意:虽然我们mybatis中不推荐一个<update></update>等出现与类型不一致的。但是是可以使用的比如:
<update id='deleteAndUpdate'>
delete T1 from table1 T1, table2 T2 where T1.a = T2.a and locate('a',`a`);
update table1 T1, table2 T2 set T1.a = 'b' where T1.a = T2.a;
</update>

IF语句的复杂句型:

mysql> select if(locate('b','abc'),'zhou','yi');
+-----------------------------------+
| if(locate('b','abc'),'zhou','yi') |
+-----------------------------------+
| zhou                              |
+-----------------------------------+
1 row in set (0.02 sec)
#locate结果返回2,为真,取前面的‘zhou’

多表关联(三表及以上):

#学生信息表
drop table if exists student;
create table `student`(
`id` int(11) not null auto_increment primary key,
`stu_number` varchar(11) not null,
`stu_name` varchar(12) not null,
`class` int(4) not null
)engine=innodb default charset=utf8 comment='学生信息表';

#学生成绩表
drop table if exists grade;
create table `grade`(
`id` int(11) not null auto_increment primary key,
`stu_number` varchar(11) not null,
`math` int(4) not null,
`chinese` int(4) not null,
`english` int(4) not null,
`total` int(5) not null
)engine=innodb default charset=utf8 comment='学生成绩表';

insert into student(stu_number,stu_name,class) values
('10000','张一',101),
('10001','张二',101),
('10002','张三',101),
('10003','张四',101),
('10004','张二',102),
('10005','张三',102),
('10006','张四',102);

insert into grade(stu_number,math,chinese,english,total) values
('10000',80,90,101,271),
('10001',82,70,101,253),
('10002',80,60,101,241),
('10003',85,10,101,196),
('10004',80,88,101,269),
('10005',87,77,101,265),
('10006',83,90,101,274);

#班级信息表
drop table if exists class;
create table `class`(
`id` int(11) not null auto_increment primary key,
`class_number` int(4) not null,
`class_banzhuren` varchar(12) not null,
`grade` int(4) not null
)engine=innodb default charset=utf8 comment='班级信息表';
insert into class(class_number,class_banzhuren,grade) values
(101,'zhouyi',1),
(102,'qian',2);

#两种关联
# 不连接
select * from student, grade, class;笛卡尔结果98select * from student s, grade g, class c where s.stu_number=g.stu_number and s.class=c.class_number; #结果7条 
select * from student s left join grade g on s.stu_number=g.stu_number  left join class c on s.class=c.class_number; #结果7条
## 这两种关联谁更好,我是不知道的

选出名为一的人,并给名为一;

mysql> select '名为一的人' as decrip,stu_number from student where locate('一',stu_name);
+-----------------+------------+
| decrip          | stu_number |
+-----------------+------------+
| 名为一的人      | 10000      |
+-----------------+------------+
1 row in set (2.26 sec)

对于order by的理解
一,两张表唯一的东西,取值别称可加可不加。
二,order by后面可接返回布尔结果,大于0或等于0,但要注意0表示反序。

mysql> select s.stu_number,stu_name,total from student s left join   grade g on s.stu_number=g.stu_number order by !locate('二',stu_name),total;
+------------+----------+-------+
| stu_number | stu_name | total |
+------------+----------+-------+
| 10001      | 张二     |   253 |
| 10004      | 张二     |   269 |
| 10003      | 张四     |   196 |
| 10002      | 张三     |   241 |
| 10005      | 张三     |   265 |
| 10000      | 张一     |   271 |
| 10006      | 张四     |   274 |
+------------+----------+-------+
7 rows in set (0.13 sec)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Improve your ability to develop, manage, and troubleshoot SQL Server solutions by learning how different components work “under the hood,” and how they communicate with each other. The detailed knowledge helps in implementing and maintaining high-throughput databases critical to your business and its customers. You’ll learn how to identify the root cause of each problem and understand how different design and implementation decisions affect performance of your systems. New in this second edition is coverage of SQL Server 2016 Internals, including In-Memory OLTP, columnstore enhancements, Operational Analytics support, Query Store, JSON, temporal tables, stretch databases, security features, and other improvements in the new SQL Server version. The knowledge also can be applied to Microsoft Azure SQL Databases that share the same code with SQL Server 2016. Pro SQL Server Internals is a book for developers and database administrators, and it covers multiple SQL Server versions starting with SQL Server 2005 and going all the way up to the recently released SQL Server 2016. The book provides a solid road map for understanding the depth and power of the SQL Server database server and teaches how to get the most from the platform and keep your databases running at the level needed to support your business. The book: • Provides detailed knowledge of new SQL Server 2016 features and enhancements • Includes revamped coverage of columnstore indexes and In-Memory OLTP • Covers indexing and transaction strategies • Shows how various database objects and technologies are implemented internally, and when they should or should not be used • Demonstrates how SQL Server executes queries and works with data and transaction log What You Will Learn Design and develop database solutions with SQL Server. Troubleshoot design, concurrency, and performance issues. Choose the right database objects and technologies for the job. Reduce costs and improve availability and manageability. Design disaster recovery and high-availability strategies. Improve performance of OLTP and data warehouse systems through in-memory OLTP and Columnstore indexes. Who This Book Is For Developers and database administrators who want to design, develop, and maintain systems in a way that gets the most from SQL Server. This book is an excellent choice for people who prefer to understand and fix the root cause of a problem rather than applying a 'band aid' to it.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值