SQL中select中使用if语句案例

SQL中select中使用if语句案例
SQL中select中使用if语句案例

selectUNITNAME,WRITER,PHONE,Real_Person_Num,

(if(MPOSTS is null,0,MPOSTS)+if(TPOSTS is null,0,TPOSTS)+if(WPOSTS is null,0,WPOSTS)) as TOTAL_POSTS,

M_Persont,T_Persont,L_Persont,

ML3,ML4,ML5,ML6,ML7,ML8,ML9,ML10,

HL,ML,LL,

TL2,TL3,TL4,TL5,TL6,TL7,TL8,TL9,TL10,TL11,TL12,TL13,

LL3,LL4,LL5,LL14,

cast(MRATIO as char) as MYMRATIO,cast(TRATIO as char) as MYTRATIO,cast(WRATIO as char) as MYWRATIO,

MPOSTS,TPOSTS,WPOSTS,

if(cast(round(ROUND(ML3/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML3/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl3,

if(cast(round(ROUND(ML4/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML4/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl4,

if(cast(round(ROUND(ML5/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML5/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl5,

if(cast(round(ROUND(ML6/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML6/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl6,

if(cast(round(ROUND(ML7/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML7/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl7,

if(cast(round(ROUND(ML8/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML8/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl8,

if(cast(round(ROUND(ML9/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML9/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mbl9,

if(cast(round(ROUND(ML10/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML10/if(MPOSTS=0,1,MPOSTS),4)*100,1) as char),"%")) as mlb10,

if(cast(round(ROUND(HL/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(HL/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as hbl,

if(cast(round(ROUND(ML/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(ML/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as mbl,

if(cast(round(ROUND(LL/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(LL/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as lbl,

if(cast(round(ROUND(TL2/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL2/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl2,

if(cast(round(ROUND(TL3/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL3/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl3,

if(cast(round(ROUND(TL4/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL4/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl4,

if(cast(round(ROUND(TL5/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL5/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl5,

if(cast(round(ROUND(TL5/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL5/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl6,

if(cast(round(ROUND(TL7/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL7/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl7,

if(cast(round(ROUND(TL8/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL8/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl8,

if(cast(round(ROUND(TL9/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL9/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tbl9,

if(cast(round(ROUND(TL10/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL10/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tlb10,

if(cast(round(ROUND(TL11/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL11/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tlb11,

if(cast(round(ROUND(TL12/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL12/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tlb12,

if(cast(round(ROUND(TL13/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(TL13/if(TPOSTS=0,1,TPOSTS),4)*100,1) as char),"%")) as tlb13,

if(cast(round(ROUND(LL3/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(LL3/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char),"%")) as wlb3,

if(cast(round(ROUND(LL4/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(LL4/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char),"%")) as wlb4,

if(cast(round(ROUND(LL5/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(LL5/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char),"%"))

as wlb5,

if(cast(round(ROUND(LL14/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char)="0.0","",concat(cast(round(ROUND(LL14/if(WPOSTS=0,1,WPOSTS),4)*100,1) as char),"%")) as wlb14

from (SELECT B.UNIT_NAME as UNITNAME,B.WRITER,B.PHONE,B.Real_Person_Num,

if(M_Persont is null,0,M_Persont) as M_Persont,

if(T_Persont is null,0,T_Persont) as T_Persont,

if(L_Persont is null,0,L_Persont) as L_Persont,

if(ML3 is null,0,ML3) as ML3,

if(ML4 is null,0,ML4) as ML4,

if(ML5 is null,0,ML5) as ML5,

if(ML6 is null,0,ML6) as ML6,

if(ML7 is null,0,ML7) as ML7,

if(ML8 is null,0,ML8) as ML8,

if(ML9 is null,0,ML9) as ML9,

if(ML10 is null,0,ML10) as ML10,

if(HL is null,0,HL) as HL,

if(ML is null,0,ML) as ML,

if(LL is null,0,LL) as LL,

if(TL2 is null,0,TL2) as TL2,

if(TL3 is null,0,TL3) as TL3,

if(TL4 is null,0,TL4) as TL4,

if(TL5 is null,0,TL5) as TL5,

if(TL6 is null,0,TL6) as TL6,

if(TL7 is null,0,TL7) as TL7,

if(TL8 is null,0,TL8) as TL8,

if(TL9 is null,0,TL9) as TL9,

if(TL10 is null,0,TL10) as TL10,

if(TL11 is null,0,TL11) as TL11,

if(TL12 is null,0,TL12) as TL12,

if(TL13 is null,0,TL13) as TL13,

if(LL3 is null,0,LL3) as LL3,

if(LL4 is null,0,LL4) as LL4,

if(LL5 is null,0,LL5) as LL5,

if(LL14 is null,0,LL14) as LL14,

if(M_Persont is null,0,M_Persont) as MRATIO,

if(T_Persont is null,0,T_Persont) as TRATIO,

if(L_Persont is null,0,L_Persont) as WRATIO,

if(ML3 is null or length(ML3)=0,0,ML3)+

if(ML4 is null or length(ML4)=0,0,ML4)+

if(ML5 is null or length(ML5)=0,0,ML5)+

if(ML6 is null or length(ML6)=0,0,ML6)+

if(ML7 is null or length(ML7)=0,0,ML7)+

if(ML8 is null or length(ML8)=0,0,ML8)+

if(ML9 is null or length(ML9)=0,0,ML9)+

if(ML10 is null or length(ML10)=0,0,ML10) as MPOSTS,

if(TL2 is null or length(TL2)=0,0,TL2)+

if(TL3 is null or length(TL3)=0,0,TL3)+

if(TL4 is null or length(TL4)=0,0,TL4)+

if(TL5 is null or length(TL5)=0,0,TL5)+

if(TL6 is null or length(TL6)=0,0,TL6)+

if(TL7 is null or length(TL7)=0,0,TL7)+

if(TL8 is null or length(TL8)=0,0,TL8)+

if(TL9 is null or length(TL2)=9,0,TL9)+

if(TL10 is null or length(TL10)=0,0,TL10)+

if(TL11 is null or length(TL11)=0,0,TL11)+

if(TL12 is null or length(TL12)=0,0,TL12)+

if(TL13 is null or length(TL13)=0,0,TL13) as TPOSTS,

if(LL3 is null or length(LL3)=0,0,LL3)+

if(LL4 is null or length(LL4)=0,0,LL4)+

if(LL5 is null or length(LL5)=0,0,LL5)+

if(LL14 is null or length(LL14)=0,0,LL14) as WPOSTS

FROM post_condition A join unit B on A.unit_code=B.unit_code where A.unit_code=$P{unitCode} and A.Is_Extra='1') AB

SQL语句大全实例

SQL语句实例 表操作 例 1 对于表的教学管理数据库中的表STUDENTS ,可以定义如下:CREATE TABLE STUDENTS (SNO NUMERIC (6, 0) NOT NULL SNAME CHAR (8) NOT NULL AGE NUMERIC(3,0) SEX CHAR(2) BPLACE CHAR(20) PRIMARY KEY(SNO)) 例 2 对于表的教学管理数据库中的表ENROLLS ,可以定义如下: CREATE TABLE ENROLLS (SNO NUMERIC(6,0) NOT NULL CNO CHAR(4) NOT NULL GRADE INT PRIMARY KEY(SNO,CNO) FOREIGN KEY(SNO) REFERENCES STUDENTS(SNO) FOREIGN KEY(CNO) REFERENCES COURSES(CNO) CHECK ((GRADE IS NULL) OR (GRADE BETWEEN 0 AND 100))) 例 3 根据表的STUDENTS 表,建立一个只包含学号、姓名、年龄的女学生表。 CREATE TABLE GIRL

AS SELECT SNO, SNAME, AGE FROM STUDENTS WHERE SEX=' 女'; 例 4 删除教师表TEACHER 。 DROP TABLE TEACHER 例 5 在教师表中增加住址列。 ALTER TABLE TEACHERS ADD (ADDR CHAR(50)) 例 6 把STUDENTS 表中的BPLACE 列删除,并且把引用BPLACE 列的所有视图和约束也一起删除。 ALTER TABLE STUDENTS DROP BPLACE CASCADE 例7 补充定义ENROLLS 表的主关键字。 ALTER TABLE ENROLLS ADD PRIMARY KEY (SNO,CNO) ; 视图操作(虚表) 例9 建立一个只包括教师号、姓名和年龄的视图FACULTY 。( 在视图定义中不能包含ORDER BY 子句) CREATE VIEW FACULTY AS SELECT TNO, TNAME, AGE FROM TEACHERS 例10 从学生表、课程表和选课表中产生一个视图GRADE_TABLE ,它包括学生姓名、课程名和成绩。 CREATE VIEW GRADE_TABLE AS SELECT SNAME,CNAME,GRADE FROM STUDENTS,COURSES,ENROLLS WHERE STUDENTS.SNO =ENROLLS.SNO AND https://www.360docs.net/doc/e916575248.html,O=https://www.360docs.net/doc/e916575248.html,O 例11 删除视图GRADE_TABLE DROP VIEW GRADE_TABLE RESTRICT 索引操作 例12 在学生表中按学号建立索引。 CREATE UNIQUE INDEX ST ON STUDENTS (SNO,ASC) 例13 删除按学号所建立的索引。 DROP INDEX ST 数据库模式操作

实验4--SQL语言--SELECT查询操作

实验4--SQL语言--SELECT查询操作 1、基于?教学管理?数据库jxgl,试用SQL的查询语句表达下列查询。(1)--检索年龄大于23的男学生的学号和姓名-- select sno,sn from s where sex='男'and age > 23 (2)--检索至少选修一门课程的女学生姓名-- select sn from S,SC where sex='女' AND S.Sno=SC.Sno group by S.Sn having count(*)>=1; (3)--检索王同学没有选修的课程的课程号-- select cno from c where https://www.360docs.net/doc/e916575248.html,o not in (select cno from sc,s where sc.sno=s.sno and sn like'王%') (4)--检索至少选修两门课程的学生学号-- select distinct s.sno from s,sc where sc.sno=s.sno group by s.sno having count(*)>=2; (5)--检索全部学生都选修的课程的课程号与课程名-- select cno,cn from c where not exists (select*from s where not exists (select*from sc where s.sno=sc.sno and https://www.360docs.net/doc/e916575248.html,o=https://www.360docs.net/doc/e916575248.html,o)) (6)--检索选修了所有3学分课程的学生学号和姓名-- select distinct s.sno,s.sn from s,sc where exists (select*from c where ct='3'and s.sno=sc.sno and https://www.360docs.net/doc/e916575248.html,o=https://www.360docs.net/doc/e916575248.html,o) 2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。 (1)--统计有学生选修的课程门数-- select count(distinct https://www.360docs.net/doc/e916575248.html,o)from sc; (2)--查询选修4号课程的学生的平均年龄-- select avg(s.age) from s,sc where s.sno=sc.sno and cno='4';

SQL常用语句+举例

SQL 常用语句+举例 相关表: 1. distinct: 剔除重复记录 例:select distinct stroe_name from Store_information 结果: 2. And / or: 并且/或 例:在表中选出所有sales 高于$1000或是sales 在$275及$500之间的记录 Select store_name ,sales from Store_information Where sales>1000 Or (sales>275 and sales <500) 3. 例:在表中查找store_name 包含 Los Angeles 或San Diego 的记录 Select * from Store_information where store_name in (‘Los Angeles ’,’San Diego ’) 结果: 4. Between : 可以运用一个范围抓出表中的值

与in 的区别:in 依照一个或数个不连续的值的限制抓出表中的值 例:查找表中介于Jan-06-1999 及Jan-10-1999 中的记录 Select * from Store_information where date between ‘Jan-06-1999’ and ‘Jan-10-1999’ 结果: 5. Like : 让我们依据一个套式来找出我们要的记录 套式通常包含: ’A_Z ’: 所有以A 开头,中间包含一个字符,以Z 结尾的字串 ’ABC%’: 所有以ABC 起头的字串 ’%XYZ ’: 所有以XYZ 结尾的字串 ’%AN%’: 所有包含AN 的字串 例:Select * from Store_information where store_name like ‘%An%’ 结果: 6. Order by: 排序,通常与ASC (从小到大,升序)、DESC (从大到小,降序)结合使用 当排序字段不止一个时,先依据字段1排序,当字段1有几个值相同时,再依据字段2排序 例:表中sales 由大到小列出Store_information 的所有记录 Select Store_name, sales,date from Store_information order by sales desc 结果: 7. 函数:AVG (平均值)、COUNT (计数)、MAX (最大值)、MIN (最小值)、SUM(求和) 语句:select 函数名(字段名) from 表名 例:求出sales 的总和 Select sum(sales) from Store_information 结果 8. COUNT (计数) 例:找出Store_information 表中 有几个store_name 值不是空的记录

mysql中select简单查询

1.简单查询select语句(1) (1) create database chapter04; use chapter04; create table exam ( id int not null primary key default null auto_increment, name varchar(20) not null, Chinese double, math double, english double ); insert into exam values(null,'关羽',85,76,70); insert into exam values(null,'张飞',70,75,70); insert into exam values(null,'赵云',90,65,95); (2) select name,english from exam; (3) select distinct english from exam; 2.简单查询select语句(2) (1)select Chinese+10,math+10,english+10 from exam; (2)select sum(Chinese+math+english) from exam where name='关羽’;

select sum(Chinese+math+english) from exam where name='张飞’; select sum(Chinese+math+english) from exam where name='赵云’; (3)select sum(Chinese+math+english) as score from exam; 3.简单查询select语句(3) (1)select name,score from exam where name='张飞'; (2)select name,english from exam where english>90; (3)select name,score from exam where score>230; 4.简单查询select语句(4) (1)select name,english from exam where english between 80 and 100; (2)select name,math from exam where math in (75,76,77); (3)insert into exam values(null,'张三',80,70,78); select score,name from exam where name like'张%'; (4)select name,score from exam where math>70 and Chinese>80; 5.聚合函数-count (1)select count (*) from exam; (2)select count (*) from exam where math>70;

50个常用sql语句实例(学生表 课程表 成绩表 教师表)

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables where table_name='Temp_Table') drop table Temp_Table go select 排名=identity(int,1,1),* INTO Temp_Table from Student go select * from Temp_Table go drop database [ ] --删除空的没有名字的数据库 问题: 1、查询“”课程比“”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student

常用SELECT语句汇总

常用SELECT语句汇总 一、单表查询 (一)按照条件查询相关记录 Select 字段1,字段2……字段N from 表 where 条件含义:从表中根据where 条件查询记录,每条记录显示的字段按照字段1、字段2….字段N的设置显示 注:select语句中的标点符号及运算符必须使用英文半角字符。 例1:从凭证库中查询2004年1月31日的凭证,每条凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段 Select 凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份 From 凭证库 where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的记录,查出的记录只显示姓名、身份证号、单位账号及个人账号 4个字段 Select 个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表 where 账户状态=’1’ 例3:从科目余额表中查询出2010年借方金额大于50万或2010年借方金额小于10万的记录,每条记录只显示摘要、科目编码、借方金额、贷方金额、年度5个字段 Select摘要,科目编码,借方金额,贷方金额,年度 From 科目余额 where(借方金额>500000 and 年度=2010) or (借方金额<100000 and 年度=2010) Select top 100 字段1,字段2……字段N from 表 where 条件含义:从表中根据where 条件查询记录,显示前100条记录,每条记录按照字段1、字段2….字段N的设置显示 例1:从凭证库中查询2004年1月31日的前100条凭证,每条 2 凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段Select top 100凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份 From 凭证库where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的前100条记录 Select top 100个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表where 账户状态=’1’ (二)通配符的使用 *表示将全部的字段内容都显示出来 例1:从业务_电子警察表中筛选出无车号或者车牌号小于3位的记录 Select * from 业务_电子警察 where 车号=’’ or Len(车号)<3 例2:从科目余额表中查询出2002年收入大于50万的记录 Select * from 科目余额 where 借方金额>500000 and 年度=2002 %表示零或多个字符 例1:从凭证库中查询2003年各月的房租收入情况 Select month(凭证日期) as 月份, sum(贷方金额) as 房租金额 from 凭证 where 摘要 like ‘%房租%’ and 年度=2003 例2:从凭证库中查询 2008年包含税的记录 Select * from 凭证库 where摘要 like ‘%税%’ and 年度=2008 _表示任何一个字符 例1:根据科目余额表查询出目编码为10开头的一级科目记录 Select * from 科目余额

Sql练习答案,sql常用实例一写就会

------1.列出至少有一个员工的所有部门。 select count(*),deptno from emp group by deptno having count(*)>1; ------2.列出薪金比“SMITH”多的所有员工。 select * from emp where sal>(select sal from emp where ename='SMITH'); ------3.列出所有员工的姓名及其直接上级的姓名。 select ename,(select ename from emp where empno=a.mgr) from emp a; select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+); ------4.列出受雇日期晚于其直接上级的所有员工。 select ename from emp a where hiredate>(select hiredate from emp where empno=a.mgr); 列出受雇日期早于其直接上级的所有员工。 select ename from emp a where hiredate<(select hiredate from emp where empno=a.mgr); ------5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。 select dname,ename from dept left join emp on dept.deptno=emp.deptno; select dname,ename from dept a,emp b where a.deptno = b.deptno(+); ------6.列出所有“CLERK”(办事员)的姓名及其部门名称。 select dname,ename from dept a,emp b where a.deptno=b.deptno and job='CLERK'; select (select dname from dept where deptno=a.deptno) as dname ,ename from emp a where job='CLERK'; ------7.列出最低薪金大于1500的各种工作。 select job,min(sal) msal from emp group by job having min(sal)>1500; ------8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。select ename from emp where deptno=(select deptno from dept where dname='SALES');

Select 查询语句

Select 查询语句 语法:SELECT [ALL|DISTINCT] <目标列表达式> [AS 列名] [,<目标列表达式> [AS 列名] ...] FROM <表名> [,<表名>…] [WHERE <条件表达式> [AND|OR <条件表达式>...] [GROUP BY 列名[HA VING <条件表达式>> [ORDER BY 列名[ASC | DESC> 解释:[ALL|DISTINCT] ALL:全部;DISTINCT:不包括重复行 <目标列表达式> 对字段可使用A VG、COUNT、SUM、MIN、MAX、运算符等 <条件表达式> 查询条件谓词 比较=、>,<,>=,<=,!=,<>, 确定范围BETWEEN AND、NOT BETWEEN AND 确定集合IN、NOT IN 字符匹配LIKE(“%”匹配任何长度,“_”匹配一个字符)、NOT LIKE 空值IS NULL、IS NOT NULL 子查询ANY、ALL、EXISTS 集合查询UNION(并)、INTERSECT(交)、MINUS(差) 多重条件AND、OR、NOT 对查询结果分组 [HA VING <条件表达式>] 分组筛选条件 [ORDER BY 列名[ASC | DESC> 对查询结果排序;ASC:升序DESC:降序 例1:select student.sno as 学号, https://www.360docs.net/doc/e916575248.html, as 姓名, course as 课程名, score as 成绩from score,student where student.sid=score.sid and score.sid=:sid 例2:select student.sno as 学号, https://www.360docs.net/doc/e916575248.html, as 姓名,A VG(score) as 平均分from score,student where student.sid=score.sid and student.class=:class and (term=5 or term=6) group by student.sno, https://www.360docs.net/doc/e916575248.html, having count(*)>0 order by 平均分DESC 例3:select * from score where sid like '9634' 例4:select * from student where class in (select class from student where name='陈小小')

SQL查询语句例子

数据表的查询(select) select 字段列表[as 别名], * from 数据表名 [where 条件语句] [group by 分组字段] [order by 排序字段列表desc] [LIMIT startrow,rownumber] 1、Select 字段列表From 数据表 例:①、select id,gsmc,add,tel from haf (* 表示数据表中所有字段) ②、select 单价,数量,单价*数量as 合计金额from haf (As 设置字段的别名) 2、Select …from …Where 筛选条件式 筛选条件式:①、字符串数据:select * from 成绩单Where 姓名='李明' ②、万用字符:select * from 成绩单Where 姓名like '李%' select * from 成绩单Where 姓名like '%李%' select * from 成绩单Where 姓名like '%李_' ③、特殊的条件式: ⑴= / > / < / <> / >= / <= ⑵AND(逻辑与) OR(逻辑或) NOT(逻辑非) ⑶Where 字段名称in(值一,值二) ⑷Where 字段名称Is Null / Where 字段名称Is Not Null 3、Select …from …group by 字段 SQL函数: SELECT sex,count(id) as women from `user` group by 'sex'; 函数名描述函数名描述 AVG平均值Count计数 MAX最大值MIN最小值 Sum求和

sql语句—实例及答案

sql 语句大致可以分类两大类SQL(结构化查询语言) 针对数据库database和表table的操作 创建create 查看show 修改alter 删除drop // 创建一个数据库 create database mydb; // 查看所有的数据库库 show databases; // 删除 mydb drop database mydb; // 删除 user 表 drop table user; 针对表中的记录的操作 增 insert 删 delete 改 update 查 select 一、操作数据库 创建一个名称为mydb1的数据库 create database mydb1; 查看当前数据库服务器中的所有数据库 show databases; 删除前面创建的mydb3数据库 drop database mydb3;

二、操作表 1. 创建表 创建员工表 Id 整形 name 字符型 sex 字符型或bit型 brithday 日期型 Entry_date 日期型 job 字符型 Salary 小数型 resume 大文本型 use database mydatabase create table employee ( id int, name varchar(20), sex varchar(6), brithday date, entry_date date, job varchar(20), salary double, resume text ); 2. 修改表 在上面员工表的基础上增加一个image列 alter table employee add colm1 int; 修改job列,使其长度为60 alter table employee modify job varchar(60); 删除sex列。 alter table employee drop sex; 表名改为users。 rename table employee to user;

常用_select_语句汇总教学总结

常用_s e l e c t_语句汇 总

常用SELECT语句汇总 一、单表查询 (一)按照条件查询相关记录 Select 字段1,字段2……字段N from 表 where 条件 含义:从表中根据where 条件查询记录,每条记录显示的字段按照字段1、字段2….字段N的设置显示 注:select语句中的标点符号及运算符必须使用英文半角字符。 例1:从凭证库中查询2004年1月31日的凭证,每条凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段 Select 凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份From 凭证库 where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的记录,查出的记录只显示姓名、身份证号、单位账号及个人账号 4个字段 Select 个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表 where 账户状态=’1’ 例3:从科目余额表中查询出2010年借方金额大于50万或2010年借方金额小于10万的记录,每条记录只显示摘要、科目编码、借方金额、贷方金额、年度5个字段 Select摘要,科目编码,借方金额,贷方金额,年度 From 科目余额where(借方金额>500000 and 年度=2010) or (借方金额<100000 and 年度=2010) Select top 100 字段1,字段2……字段N from 表 where 条件

含义:从表中根据where 条件查询记录,显示前100条记录,每条记录按照字段1、字段2….字段N的设置显示 例1:从凭证库中查询2004年1月31日的前100条凭证,每条凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段 Select top 100凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份 From 凭证库 where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的前100条记录 Select top 100个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表 where 账户状态=’1’ (二)通配符的使用 *表示将全部的字段内容都显示出来 例1:从业务_电子警察表中筛选出无车号或者车牌号小于3位的记录 Select * from 业务_电子警察 where 车号=’’ or Len(车号)<3 例2:从科目余额表中查询出2002年收入大于50万的记录 Select * from 科目余额 where 借方金额>500000 and 年度=2002 %表示零或多个字符 例1:从凭证库中查询2003年各月的房租收入情况 Select month(凭证日期) as 月份, sum(贷方金额) as 房租金额 from 凭证 where 摘要 like ‘%房租%’ and 年度=2003 例2:从凭证库中查询 2008年包含税的记录 Select * from 凭证库 where摘要 like ‘%税%’ and 年度 =2008 _表示任何一个字符 例1:根据科目余额表查询出目编码为10开头的一级科目记录Select * from 科目余额 where 科目编码 like ‘10_’

SQL语句——SELECT查询操作

实验报告 【实验名称】SQL语言——SELECT查询操作 姓名:学号:班级:日期:10月26日 【实验目的】 1.掌握SQL程序设计基本规范,熟练运用SQL语言实现数据基本查询,包括单表查询、 分组统计查询和连接查询。 2. 掌握SQL嵌套查询和集合查询等各种高级查询的设计方法等。 【实验环境】 SQL SERVER 2008 【实验内容】 1.针对SQL Server数据库设计各种单表查询语句、分组统计查询语句;设计单个表针 对自身的连接查询,设计多个表的连接查询。理解和掌握SQL查询语句各个子句的特点和作用,按照SQL程序设计规范写出具体的SQL查询语句,并调试通过。 2. 针对SQL Server数据库,正确分析用户查询要求,设计各种嵌套查询和集合查询。 【实验代码】 --在选课系统下进行以下查询 --单表查询 SELECT * FROM Student SELECT Sname,2014-Sage FROM Student SELECT Sname,'Year of Birth:',2014-Sage,LOWER (Sdept) FROM Student SELECT Sname NAME,'Year of Birth:'BIRTH,2014-Sage BIRTHDAY,LOWER(Sdept)DEPARTMENT --改变查询结果的列标题 FROM Student SELECT DISTINCT Sno --去掉结果表中的重复行 FROM SC SELECT Sname,Sdept,Sage FROM Student WHERE Sdept IN ('CS','MA','IS') --确定集合

SQL中经典函数使用实例大全

select substring(CODE_KIND,2,3)kind from code //2处为起始位置,3为获取字符的长度。 select left(CODE_KIND,3)from CODE //返回从左边数起的3个字符,right()函数反之。 select left('1233',2) select upper(code_name)name from code //将字符串中每个小写字母转换为大写字母,lower 转化成小写。 select REVERSE(code_name)code_name from code //将查询结果反过来。 SELECT row_number()over (order by code_kind)as kind,*from code

with codeT as ( SELECT row_number()over (order by code_kind desc)as kind,*from code ) select*from codeT where kind>1 and kind<4 SELECT RANK()over (order by code_kind)as kind,*from code //rand() 查询出来的数字不是连续的。DENSE_RANK()查出来的反之。 SELECT DENSE_RANK()over (order by code_kind)as kind,*from code

學習SQL中事務,以及upper(),right(),RANK(),DENSE_RANK(),replace()等等一些操作sql中字符串的常用函數。可快速查找出sql中符合自己所想要的數據。 可以。比如说现在有两个排序字段,如果根据第1个字段排不出来(有几条记录的这个字段相同)的话,再根据第2个字段排的。 请看下面的例子: 可多字段排序,当多字段排序时,首先排序第一个字段,当第一个字段值相同时,才按第二个字段排序,如果第二个字段值相同,才按第三个排序... 比如说现在有两个排序字段,如果根据第1个字段排不出来(有几条记录的这个字段相同)的话,再根据第2个字段排的 排序前(注意红色标记部分的数据): SQL语句: select * from Test where uname like 'Test%' order by score desc, uname asc; 排序后(注意红色标记部分的数据):

SQL查询语句SELECT精华

一、简单查询 简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的 表或视图、以及搜索条件等。 例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。 SELECT nickname,email FROM testtable WHERE name='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表、星号、表达式、变量(包括局部变 量和全局变量)等构成。 1、选择所有列 例如,下面语句显示testtable表中所有列的数据: SELECT * FROM testtable 2、选择部分列并指定它们的显示次序 查询结果集合中数据的排列顺序与选择列表中所指定的列名排列顺序相同。 例如: SELECT nickname,email FROM testtable 3、更改列标题 在选择列表中,可重新指定列标题。定义格式为: 列标题=列名 列名列标题 如果指定的列标题不是标准的标识符格式时,应使用引号定界符,例如,下列语句使用汉字显示列 标题: SELECT 昵称=nickname,电子邮件=email FROM testtable 4、删除重复行 SELECT语句中使用ALL或DISTINCT选项来显示表中符合条件的所有行或删除其中重复的数据行,默认 为ALL。使用DISTINCT选项时,对于所有重复的数据行在SELECT返回的结果集合中只保留一行。 5、限制返回的行数 使用TOP n [PERCENT]选项限制返回的数据行数,TOP n说明返回n行,而TOP n PERCENT时,说明n是

sql常用语句100例

--update phoneinfo set cityname = '克孜勒苏柯尔克孜' where cityname = '克孜勒苏柯尔克孜州' --update phoneinfo set cityname = '湘西' where pad1 = '湖南 吉首' select * from dbo.PhoneInfo --update dbo.PhoneInfo set provincename=b.provincename,cityname=b.cityname from dbo.PhoneInfo a,PhoneInfo_hl b --where a.phonebound=b.phonebound --select * from dbo.UnknowPhoneBound --select * from dbo.Area --select * from phoneinfo a, phoneinfo_old b, phoneinfo_hl c where a.phonebound = b.phonebound and a.phonebound = c.phonebound and (a.cityname <> b.cityname or a.cityname <> c.cityname) --select * from phoneinfo a, phoneinfo_hl b where a.phonebound = b.phonebound and a.cityname <> b.citynameselect * from phoneinfo a, phoneinfo_old b where a.phonebound = b.phonebound and a.cityname <> b.cityname --select * into phoneinfo_bak from phoneinfo --select * from phoneinfo_bak select * from phoneinfo a, phoneinfo_old b where a.phonebound = b.phonebound and a.cityname <> b.cityname --select * from dbo.PhoneInfo_Telecom --update PhoneInfo_Telecom set provincename = '内蒙古' where pad1 = '内蒙兴安盟' update PhoneInfo set cityname = '酒泉' where pad1 = '甘肃 酒泉嘉峪关' --update dbo.PhoneInfo_old set provincename=b.provincename,cityname=b.cityname from dbo.PhoneInfo_old a,PhoneInfo_Telecom b --where a.phonebound=b.phonebound create table client_all as

SQL查询语句讲解实例

通用SQL数据库查询语句精华集2007年12月18日星期二08:18简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句 一、简单查询 简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的表或视图、以及搜索条件等。 例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。SELECT nickname,email FROM testable WHERE name='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表、星号、表达式、变量(包括局部变量和全局变量)等构成。 1、选择所有列 例如,下面语句显示testtable表中所有列的数据: SELECT * FROM testtable 2、选择部分列并指定它们的显示次序 查询结果集合中数据的排列顺序与选择列表中所指定的列名排列顺序相同。 例如: SELECT nickname,email FROM testtable 3、更改列标题 在选择列表中,可重新指定列标题。定义格式为: 列标题=列名 列名列标题 如果指定的列标题不是标准的标识符格式时,应使用引号定界符,例如,下列语句使用汉字显示列标题: SELECT 昵称=nickname,电子邮件=email

FROM testtable 4、删除重复行 SELECT语句中使用ALL或DISTINCT选项来显示表中符合条件的所有行或删除其中重复的数据行,默认为ALL。使用DISTINCT选项时,对于所有重复的数据行在SELECT返回的结果集合中只保留一行。 5、限制返回的行数 使用TOP n [PERCENT]选项限制返回的数据行数,TOP n说明返回n行,而TOP n PERCENT 时,说明n是表示一百分数,指定返回的行数等于总行数的百分之几。 例如: SELECT TOP 2 *FROM testtable SELECT TOP 20 PERCENT * FROM testtable (二)FROM子句 FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图,它们之间用逗号分隔。 在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列所属的表或视图。例如在usertable和citytable表中同时存在cityid列,在查询两个表中的cityid时应使用下面语句格式加以限定: SELECT username,citytable.cityid FROM usertable,citytable WHERE usertable.cityid=citytable.cityid 在FROM子句中可用以下两种格式为表或视图指定别名: 表名as 别名 表名别名 例如上面语句可用表的别名格式表示为: SELECT username,b.cityid FROM usertable a,citytable b WHERE a.cityid=b.cityid SELECT不仅能从表或视图中检索数据,它还能够从其它查询语句所返回的结果集合中查询

select查询语句用法

sql server 2005 学习心得(select查询语句用法) select*from userInfo where age like'2[25]' 功能:查询userInfo表中age字段,所有以2开头,且第二位是2或5的记录。 select*from userInfo where name like '_娜_' 功能:查询userInfo表中name(char(6))字段所有中间一个字是“娜”的记录。下划线“_”代表的是两个任意字符,如上:名为“谢娜娜”或“谢娜”等的记录将会被查询出来 select*from userInfo where address like '%川%' 功能:查询userInfo表中address字段所有包含“川”字的记录 select*from userInfo where name like '[m-n]%' 功能:查询所有userInfo表中所有name字段是以m或n开头的记录 select*from userInfo where name like 'm[^i]%' 功能:查询userInfo表中name字段所有以m开头、第2个字母不是字母c的名称select*from userInfo where ID in(1,2) 功能:查询userInfo表中所有ID等于1或者2的记录 等同于 select * from userInfo where ID='1' or ID='2' select*from userInfo where memo like '20/%' escape '/'

功能:查询memo字段值为20%的所有记录,但是在sql Server里面%为通配符如果不加就会有其他意思 例如:select * from userInfo where memo like '2%' 就跟上面查到一样的记录,这一句的功能是查到memo字段的值以2开头的记录。20%也是以2开头的。这样就会出现问题。所以用转义字符还是很必要的。 select count(distinct ProdID) '产品种类' from userInfo 功能:查询出来有多少种产品,distinct就是查询不重复的记录,加上count()就是不重复的记录的总数了

相关文档
最新文档