‘壹’ sql怎么用查询结果作为条件进行查询
1、查询数据库表的所有字段并直接使用select语句。从数据库表中选择*。
‘贰’ SQL语句where多条件查询怎么写
工具/材料:以Management Studio为例。
1、首先在桌面上,点击“Management Studio”图标。
‘叁’ sql 分页多条件查询
查询可以用if else 去做。
例如:
sql="select * from ........."
``````````````
xh=request("型号")
······
if xh<>"" then sql=sql+" and 型号='"&xh&"'"
分页可以用
<%TurnPage(rs,20,"型号="&xh&"&其他6个参数·······")%>
<%
Sub TurnPage(ByRef Rs_tmp,PageSize,canshu) 'Rs_tmp 记录集 PageSize 每页显示的记录条数;
Dim TotalPage '总页数
Dim PageNo '当前显示的是第几页
Dim RecordCount '总记录条数
Rs_tmp.PageSize = PageSize
RecordCount = Rs_tmp.RecordCount
TotalPage = INT(RecordCount / PageSize * -1)*-1
PageNo = Request.QueryString ("PageNo")
'直接输入页数跳转;
If Request.Form("PageNo")<>"" Then PageNo = Request.Form("PageNo")
'如果没有选择第几页,则默认显示第一页;
If PageNo = "" then PageNo = 1
If RecordCount <> 0 then
Rs_tmp.AbsolutePage = PageNo
End If
'获取当前文件名,使得每次翻页都在当前页面进行;
Dim fileName,postion
fileName = Request.ServerVariables("script_name")
postion = InstrRev(fileName,"/")+1
'取得当前的文件名称,使翻页的链接指向当前文件;
fileName = Mid(fileName,postion)
response.write "<table border=0 width='100%'><tr> "
If RecordCount = 0 or TotalPage = 1 Then
Response.Write ""
Else
response.write "<td align=left style='font-size:12px'> 总页数:<font color=#ff3333>"&TotalPage&"</font>页"
response.write "当前第<font color=#ff3333>"&PageNo&"</font>页 </td> "
response.write "<td align='right' style='font-size:12px'> "
end if
If RecordCount = 0 or TotalPage = 1 Then
Response.Write ""
Else
response.write "<a href='"&fileName&"?PageNo=1&"&canshu&"'>首页|</a>"
If PageNo - 1 = 0 Then
Response.Write "前页|"
Else
response.write "<a href='"&fileName&"?PageNo="&PageNo-1&"&"&canshu&"'>前页|</a>"
End If
If PageNo+1 > TotalPage Then
Response.Write "后页|"
Else
response.write "<a href='"&fileName&"?PageNo="&PageNo+1&"&"&canshu&"'>后页|</a>"
End If
response.write "<a href='"&fileName&"?PageNo="&TotalPage&"&"&canshu&"'>末页</a>"
End If
response.write "</td></td></tr></table> "
end sub%>
‘肆’ 怎样在SQL数据库中实现多条件查询
`
主要就是在where后后使用and逻辑运算符
如:
select * from [表名] where 学校='清华大学' and 住址='北京' and 性别='男'
以上为查询,清华大学,住址为北京的所有男性的信息
还可以使用用模糊查询.
如:
select * from [表名] where 学校 like '%清华大学%' and 住址 like '%北京%' and 性别='男'
以上为查询学校有清华两字,住址中有北京两字的所有男性的信息
要是回答的内容有问题,或认为不妥,请发送网络消息给我,消息内容加上本页网址哦。。
·
‘伍’ SQL中判断分支语句怎样写,帮忙
不同的数据库语法有点不同,一般SQL应如下写法:
select (CASE WHEN ( t1.PRICE is null ) then v.Selling2 else t1.PRICE end) as "PRICE"
from AGENTGOODSCHECK t1 left join V_AGENTGOODSMASTER v
on t1.GOODSCODE=v.OrderCode
where CONVERT (VARCHAR(10),DATEADD(DAY,-1,t1.VDATE),111) = '2011/11/24'
and t1.GOODSCODE='000809'
and t1.STATUS<>'03'
‘陆’ sql查询怎么进行(多条件分组判断汇总)
sql多条件分组查询举例:
比如有两张表:
表一:
姓名 分数
——————
李一 2
李二 1
李三 1
张一 3
张二 2
张三 2
……
表二:
姓
——
李
张
王
要查询表二中的姓的数据对表一进行分组,然后将分数统计出来。
sql语句写法如下:
SELECT
b. NAME,
sum(a.score) AS 分数
FROM
tb1 a
LEFT JOIN tb2 b ON SUBSTR(a.name FROM 1 FOR 1)= b.`name`
GROUP BY
b. NAME;
这个是Mysql的写法 Oracle把SubStr函数改一下就可以了。
‘柒’ SQL语句分条件查询
select
*
from
table
where
(a
is
null
and
b=c)
or
(a
is
not
null
and
b=d)
把两个where条件用or连接起来就行了,不管几个,只是if
else关系就用or连接,逻辑关系是一样的,不信你自己仔细看
‘捌’ SQL分组且根据不同的条件查询。
SELECT 名称, 数量+(SELECT SUM(数量) FROM B表 WHERE A表.ID=B表.ID AND A表.时间<B表.时间)
FROM A表
‘玖’ 如何在sql server中进行有条件的查询
查询记录
--------------------------------------------------------------------------------
一般查询
SELECT [DISTINCT] <column1 [as new name] ,columns2,...>
FROM <table1>
[WHERE <条件>]
[GROUP BY <column_list>]
[HAVING <条件>]
[ORDER BY <column_list> [ASC|DESC]]
DISTINCT --表示隐藏重复的行 会去除所有要查询的列完全重复的行
WHERE --按照一定的条件查找记录
GROUP BY --分组查找(需要汇总时使用)
HAVING --分组的条件
ORDER BY --对查询结果排序
例:
select distinct sal ,empno,deptno from emp where deptno is not null group by deptno,empno,sal order by deptno;
select distinct deptno from emp;
要显示全部的列可以用*表示
例:
select * from emp;
WHERE 语句的运算符
where <条件1>AND<条件2> --两个条件都满足
例:
select * from emp where deptno=10 and sal>1000;
where <条件1>OR<条件2> --两个条件中有一个满足即可
例:
select * from emp where deptno=10 OR sal>2000;
where NOT <条件> --不满足条件的
例:
select * from emp where not deptno=10;
where IN(条件列表) --所有满足在条件列表中的记录
例:
select * from emp where empno in(7788,7369,7499);
where BETWEEN .. AND.. --按范围查找
例:
select * from emp where sal between 1000 and 3000;
where 字段 LIKE --主要用与字符类型的字段
例1:
select * from emp where ename like '_C%'; --查询姓名中第二个字母是'C'的人
'-' 表示任意字符;
'%' 表示多字符的序列;
where 字段 IS [NOT] NULL --查找该字段是[不是]空的记录
汇总数据是用的函数
SUM --求和
例:
select deptno,sum(sal) as sumsal from emp GROUP BY deptno;
AVG --求平均值
MAX --求最大值
MIN --求最小值
COUNT --求个数
子查询
SELECT <字段列表> from <table_name> where 字段 运算符(<SELECT 语句>);
例:
select * from emp where sal=(select max(sal) from emp);
运算符
Any
例:
select * from emp where sal>ANY(select sal from emp where deptno=30) and deptno<>30;
--找出比deptno=30的员工最低工资高的其他部门的员工
ALL
select * from emp where sal>ALL(select sal from emp where deptno=30) and deptno<>30;
--找出比deptno=30的员工最高工资高的其他部门的员工
连接查询
SELECT <字段列表> from table1,table2 WHERE table1.字段=table2.字段
例
select empno,ename,dname from emp,dept where emp.deptno=dept.deptno;
查询指定行数的数据
SELECT <字段列表> from <table_name> WHERE ROWNUM<行数;
例:
select * from emp where rownum<=10;--查询前10行记录
注意ROWNUM只能为1 因此不能写 select * from emp where rownum between 20 and 30;
这中情况要使用子查询先小于30且将rownum这列显示 然后再使rownum大于20
例:
select * from (select rownum rnum,emp.* from emp where rownum<30) where rnum>20;
要查第几行的数据可以使用以下方法:
select * from emp where rownum<=30 and empno not in (select empno from emp where rownum<=20);
结果可以返回整个数据的20-30行;
不过这种方法的性能不高;