sql命令小结
sqlplus命令
Q1: 用sql命令查询数据
where条件表达式
--统计函数
复制代码代码如下:
Select count(1) from student;
--like模糊查询
--统计班上姓张的人数
复制代码代码如下:
select count(*) from student where realName like '张%';
--统计班上张姓两个字的人数
复制代码代码如下:
select count(*) from student where realName like '张_';
--统计班上杭州籍的学生人数
复制代码代码如下:
select count(*) from student where home like '%杭州%';
--查询班上每位学生的年龄
复制代码代码如下:
select realName,year(now())-year(birthday) as age from student;
--查询90年出生的学生
复制代码代码如下:
select realName from student where year(birthday)>='1990';
--查询1987-1990年出生的学生
复制代码代码如下:
select realName from student where year(birthday)<='1990' and year(birthday)>='1987';
select * from student where year(birthday) between '1987' and '1990';
--查询班上男女生人数
复制代码代码如下:
select sex,count(*) from student group by sex;
--in子句查询班上B或O型血的学生
复制代码代码如下:
select realName,blood from student where blood in('B','O');
子查询
子查询也可称之为嵌套查询,有些时候,一次查询不能解决问题,需要多次查询。
按子查询返回的记录行数区分,可分为单行子查询和多行子查询;
复制代码代码如下:
select * from emp where sal>(select sal from emp where ename='ALLEN‘ or ename =‘KING')
Q2: 用SQL命令插入一个记录
sql 命令插入新记录使用 insert into 字句。
一、普通用法,指定值插入:
INSERT INTO 表名称 VALUES (值1, 值2,....)
或
INSERT INTO 表名称 (列1, 列2,...) VALUES (值1, 值2,....)
示例如下:
insertintotablevalues("1","a","b")
二、读取表中值插入另一个表中,或新建一个表:
1、INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:
insertintotable2select*fromtable1
2、SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
SELECTa,bintoTable2fromTable1
小提示:内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。