當前位置:首頁 » 編程語言 » sql分支條件查詢
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql分支條件查詢

發布時間: 2023-01-15 09:26:12

『壹』 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行;
不過這種方法的性能不高;