sql数据查询
SELECT语句的基本形式为
SELECT [ALL|DISTINCT]<目标列表达式>[,<目标列表达式>···]
FROM <表名或视图名>[,<表名或视图名>···]
[WHERE <条件表达式>]
语句的功能是根据WHERE子句中的条件表达式,从基本表(或视图)中找出满足条件的元组,按SELECT子句中的目标列,选出元组中的目标列形成结果表。
SQL语句对数据库的操作十分灵活方便,原因在于SELECT语句中的成分丰富多样,有许多可选形式,尤其是目标列和目标表达式,下面用例子来详细说明,例子涉及的是"学生-课程"数据库,其关系模式集如下:
学生信息表Student(SNO,SNAME,SSEX,SBIRTHDAY CLASS)
教师信息表Teacher(TNO,TNAME,TSEX,TBIRTHDAY,DEPART)
课程信息表Course(CNO,CNAME,TNO)
成绩表Grade(SNO,CNO,DEGREE)
例题:查询Student表所有学生信息。
SELECT * FROM Student;/*选择操作*/
例题:查询Student表中所有学生的学号和姓名。
SELECT SNO,SNAME FROM Student;/*投影操作*/
例题:查询Grade表中成绩在60到80之间的所有记录。
SELECT * FROM Grade WHERE degree BETWEEN 60 AND 80;/*选择操作*/
例题:查询Grade表中成绩为85、86、88的记录。
SELECT * FROM Grade WHERE degree IN(85,86,88);/*选择操作*/
例题:查询所有学生的SNAME,CNAME和DEGREE。
SELECT Student.SNAME,Course.CNAME,Grade.DEGREE
FROM Student,Course,Grade
WHERE Student.SNO = Grade.SNO,Grade.CNO = Course.CNO;/*连接操作*/
例题:查询"张旭"教师任课的学生成绩。
SELECT CNO,SNO,DEGREE FROM Grade
WHERE CNO=(SELECT Course.CNO FROM Course,Teacher
WHERE Course.TNO=Teacher.TNO and Teacher.TNAME="张旭")
此查询操作为嵌套查询。子查询在上一级查询处理之前求解,即嵌套查询是从里向外处理的,这样,外层查询可以利用内层查询的结果,在此例中首先执行子查询:
SELECT Course.CNO FROM Course,Teacher
WHERE Course.TNO=Teacher.TNO and Teacher.TNAME="张旭"
获得张旭老师讲授课程的课程号(801003),然后执行外查询:
SELECT CNO,SNO,DEGREE FROM Grade
WHERE CNO="801003"
获得"张旭"教师任课的学生成绩。
❷ SQL数据查询
--createtable
createtable#price_table(IDint,AddTimedatetime,Pricedecimal);
--insertdata
insertinto#price_tablevalues(1,'2015-03-0100:00:00',256);
insertinto#price_tablevalues(2,'2015-02-0800:00:00',333);
insertinto#price_tablevalues(3,'2014-04-0200:00:00',444);
insertinto#price_tablevalues(4,'2015-03-1811:12:06',777);
--selecttable
select*from#price_table;
DECLARE@StartDateDATETIME='2015-03-01'
DECLARE@EndDateDATETIME,
@LastMonStartDateDATETIME,
@LastYearStartDateDATETIME,
@LastYearEndDateDATETIME
SELECT@EndDate=CONVERT(VARCHAR(10),DATEADD(mm,1,@StartDate)),
@LastMonStartDate=CONVERT(VARCHAR(10),DATEADD(mm,-1,@StartDate)),
@LastYearStartDate=CONVERT(VARCHAR(10),DATEADD(yy,-1,@StartDate)),
@LastYearEndDate=CONVERT(VARCHAR(10),DATEADD(mm,1,@LastYearStartDate))
SELECTISNULL(a.currentmonth,0)AScurrentmonth,ISNULL(b.lastmonth,0)ASlastmonth,ISNULL(c.lastyear,0)ASlastyear
FROM(selectSUM(price)AScurrentmonthFROM#price_tablea WHEREa.AddTime>[email protected]<@EndDate)a,
(selectSUM(price)ASlastmonthFROM#price_tablea WHEREa.AddTime>[email protected]<@StartDate)b,
(selectSUM(price)ASlastyearFROM#price_tablea WHEREa.AddTime>[email protected]<@LastYearEndDate)c
看下直接建个存储过程来解决罗,参数就@StartDate 如果你方便的话传当月1号,不然你坚持要 2015-03 这种,手工处理一下也可以
❸ sql数据库中检索数据
这个可能复杂点,我有一个方案,
首先做一个自定义方法,然后进行查询
-----自定义方法------
CREATE FUNCTION SplitStr (@splitString varchar(8000), @separate varchar(10))
RETURNS @returnTable table(col_Value varchar(20))
AS
BEGIN
declare @thisSplitStr varchar(20)
declare @thisSepIndex int
declare @lastSepIndex int
set @lastSepIndex = 0
if Right(@splitString ,len(@separate)) <> @separate set @splitString = @splitString + @separate
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)
while @lastSepIndex <= @thisSepIndex
begin
set @thisSplitStr = SubString(@splitString ,@lastSepIndex,@thisSepIndex-@lastSepIndex)
set @lastSepIndex = @thisSepIndex + 1
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)
insert into @returnTable values(@thisSplitStr)
end
return
END
---------测试语句--------
select * from
(
select 'a,b,c,a,d' as b
union all
select 'c,f,d,e,t'
union
select 'd,d,f,g,h'
) as a
where
(select COUNT(1)
from
dbo.SplitStr(b,',')
group by col_Value
having COUNT(1)>1)>0
--------查询结果--------
a,b,c,a,d
d,d,f,g,h
❹ sql数据库查询语句
表结构如何?
❺ sql数据查询
第一种写法,比较好理解:
declare
@numint
set@num=0
selectcase
whenrowindexin(1,2)
then@num=@num+score
else@num=@num/2+scoreend
from(
selectrow_number()over(orderbyid)rowindex,*from表名
)t
select@num--得到结果
第二种写法,把算式拆分,可以发现规律,第一条记录后面(包含自己)还有4条记录,除以2的3次方,这条记录除外,其他的记录都是后面有记录(包含自己),就除以2的几次方,假设以id的升序为计算数列,则计算如下:
declare
@numint
select@num=count(*)from表名
selectsum(
casewhenrowindex=@num
thenscore/power(2,rowindex-1)
elsescore/power(2,rowindex)end
)
from(
selectrow_number()over(orderbyiddesc)rowindex,*from表名--降序查询
)t
❻ SQL数据查询功能
多表查询才会使用join进行表的连接,on关键字用来描述连接条件;
如果只是单表的查询,自然就不需要连接多表了
特殊情况下会进行表的自身连接,这时可以将一个表看作两个虚拟表
❼ SQL数据库查询
有很多解决办法
直接用a.表001 join b.表002
或者,把a库中的表001导入到b库中,然后在一个库中来查询。
❽ 如何根据sql数据库表中数据进行查询
sql server 查询一个表的所有信息:查询语法为:select * from 表名 语法中的”*“代表所有。
实际操作举例:某数据中有一表叫”user_user“,表中存储了一些用户信息;
1、查询”user_user“表中所有人员信息且所有字段,sql语句:select * from user_user
2、查询”user_use“表中所有人员信息且部分字段。sql语句:select user_show_id,user_name,pinyin,password from user_user
3、条件查询”user_user“表,以user_name为查询条件查询所有信息。sql语句:select * from user_user where user_name='李丹'
4、模糊查询”user_user“表,以user_name为模糊查询条件查询所有信息。sql语句:select * from user_user where user_name like '%张%'
❾ SQL 数据库查询
不需要这么麻烦的。运行下面的语句即可:
declare @str varchar(100)
set @str='xxxxx' --这地方放你所查询的关键字;
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'')
print ''所在的表及字段: ['+b.name+'].['+a.name+']'''
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0
and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb