若開放使用者查one name、two name、three name的欄位,有時使用者只查租或只查買或租和買都查SQL要怎麼下?
租和買的two name、three name的代號有的相同但是不同的東西
若開放使用者查one name、two name、three name的欄位,有時使用者只查租或只查買或租和買都查SQL要怎麼下?
請問SQL 可以下判斷式若是租的話執行SQL1,若是買的話執行SQL2,若租及買都查執行SQL3?
假設資料在 #TB_TEMP
猜測使用者的查詢( ONE_NAME, TWO_NAME, THREE_NAME )
會傳入對應代碼當參數 ( ONE, TWO, THREE )
各個欄位若想查詢全部資料可以考慮回傳其他代碼 ( 例如:0 )
例如:
DECLARE @ONE INT, @TWO INT, @THREE INT
SET @ONE = 0 -- 0:ALL 1:租 2:買
SET @TWO = 0 -- 0:ALL (1,2)
SET @THREE = 2 -- 0:ALL (1~5)
SELECT * FROM #TB_TEMP
WHERE ( ( ONE = @ONE OR @ONE = 0 ) AND
( TWO = @TWO OR @TWO = 0 ) AND
( THREE = @THREE OR @THREE = 0 ) )
SELECT *
FROM table
WHERE one IN (1,2) AND -- 只租: (1),只買: (2),租或買:(1,2)
two = two的代號 AND
three = three的代號
//只查租
SELECT * FROM TABLENAME WHERE one_name="租"
//只查買
SELECT * FROM TABLENAME WHERE one_name="買"
//查租或買
SELECT * FROM TABLENAME WHERE one_name="租" OR one_name="買"
另外有個小小建議
one, two, three 三個欄位都可以取消不要
這個 Table 好像怪怪的,第 4 行
1 租 1 書 3 雜誌 PC HOME
若也可以"買",不就變成
2 買 1 書 3 雜誌 PC HOME
第 9 行
2 買 1 書 3 生活書 園藝
那麼 2,1,3 是要對應到 "雜誌 PC HOME" 還是 "生活書 園藝" ?
個人猜測
one 對 one name
two 對 two name
three 對 three name
item name 是名稱
我發現他發問都喜歡讓別人通靈他的問題
如果哪天他當上PM,下面的工程師遲早會被他氣死
以下範例純屬臆測
Demo
同感,而且我覺得大家都好友善啊!怎麼會有力氣去參透他的問題呢? XD
請問SQL 可以下判斷式若是租的話執行SQL1,若是買的話執行SQL2,若租及買都查執行SQL3?
這部份不歸SQL管,SQL負責新增&刪除&修改&查詢你的資料庫
三個獨立的SQL就是三個獨立的SQL,判斷要執行哪一個SQL的是在他之外的東西,這東西通常叫做後端
然後叫後端作業的人(也就是要資料)是前端,所以這邊判斷你的問題點也許不在SQL
而是對基本的介面不瞭解
這部份的問題不訪根據你使用的語言發問,且如果沒有自信說明完整的話,可以在不透漏隱私的程度下提供程式碼,這樣就不會因為看不懂別人給的答案,結果自己問的也不是這個問題導致浪費時間喔!
如果都只在資料庫的話..
declare @one_name nvarchar(10)
declare @keyword nvarchar(50)
declare @Tmp1 table(
one int
,[one name] nvarchar(50)
)
insert into @Tmp1
values(1,'租')
,(2,'買')
declare @Tmp2 table(
two int
,[two name] nvarchar(50)
)
insert into @Tmp2
values(1,'書')
,(2,'工具類')
declare @Tmp3 table(
two int
,three int
,[three name] nvarchar(50)
)
insert into @Tmp3
values(1,1,'童書')
,(1,2,'電腦書')
,(1,3,'雜誌')
,(1,4,'財經書')
,(1,5,'其他')
,(2,4,'工具類')
declare @Tmp4 table(
id int
,two int
,three int
,[item name] nvarchar(50)
)
insert into @Tmp4
values(1,1,1,'白雪公主')
,(2,1,2,'Win10 手冊')
,(3,1,3,'Pchome')
,(4,1,4,'今周刊')
,(5,1,5,'資產管理')
,(6,1,1,'獅子王')
,(7,1,2,'Office 2016')
,(8,1,4,'園藝')
,(9,2,4,'十字起子')
set @one_name = '租,買'
set @keyword = '電腦書'
select one
,[one name]
,a.two
,[two name]
,a.three
,[three name]
,[item name]
from @Tmp4 a
left join @Tmp3 b on b.two = a.two and b.three = a.three
left join @Tmp2 c on b.two = c.two
,@Tmp1
where CharIndex([one name],@one_name) > 0
and (1=2
or [two name] like '%' + @keyword + '%'
or [three name] like '%' + @keyword + '%'
)
order by one
set @one_name = '租'
set @keyword = '電腦書'
select one
,[one name]
,a.two
,[two name]
,a.three
,[three name]
,[item name]
from @Tmp4 a
left join @Tmp3 b on b.two = a.two and b.three = a.three
left join @Tmp2 c on b.two = c.two
,@Tmp1
where CharIndex([one name],@one_name) > 0
and (1=2
or [two name] like '%' + @keyword + '%'
or [three name] like '%' + @keyword + '%'
)
order by one
[資料內容]
Book_MethodSales = 1 租
Book_MethodSales = 2 買
[JSON] 查詢條件 為 共同條件
BookName = 書籍名稱
Lease = 1 租
Sales = 2 買
[基本語法]
DECLARE @_InBox_JSON_Source nvarchar(Max)='[
{
"ProgID":"Books",
"ProgVersion":"1.0",
"ProgAction":"Search",
"ProgDevice":"Sony",
"ProgDeviceOS":"Android",
"ProgDeviceLocale":"Taiwan",
"ProgDeviceHardware":"???",
"ProgServiceCode":"",
"SearchInfo":{
"BookName":"PCHOME",
"MethodSales":{
"Lease":1,
"Sales":null
}
}
]'
--取得要查詢的項目
DECLARE @_InBox_Search_BookNAME nvarchar(60) =null
DECLARE @_InBox_Search_Method_Lease tinyint =null
DECLARE @_InBox_Search_Method_Sales tinyint =null
DECLARE @_OutBox_SearchTSQLScript nvarchar(Max) =null
SET @_InBox_Search_BookNAME = JSON_VALUE(@_InBox_JSON_Source,'$[0].SearchInfo.BookName')
if(CONVERT(bit,JSON_VALUE(@_InBox_JSON_Source,'$[0].SearchInfo.MethodSales.Lease'))=1)
Begin
SET @_InBox_Search_Method_Lease=1
END
if(CONVERT(bit,JSON_VALUE(@_InBox_JSON_Source,'$[0].SearchInfo.MethodSales.Sales'))=1)
Begin
SET @_InBox_Search_Method_Sales=2
END
if(@_InBox_Search_BookNAME is not null)
Begin
SELECT '條件一'
SET @_OutBox_SearchTSQLScript=(
SELECT
Book_NAME AS 'Books.NAME' ,
Book_TypesInfo AS 'Books.TypesInfo' ,
Book_ClassInfo AS 'Books.ClassInfo' ,
REPLACE(REPLACE(Book_MethodSales,'1','租'),'2','買') AS 'Books.Sales'
FROM BooksInfo WITH(NOLOCK)
WHERE
(
Book_MethodSales IN (@_InBox_Search_Method_Lease)
or
Book_MethodSales IN (@_InBox_Search_Method_Sales)
)
AND Book_NAME = @_InBox_Search_BookNAME
FOR JSON PATH,INCLUDE_NULL_VALUES
)
End
else
Begin
SELECT '條件二'
SET @_OutBox_SearchTSQLScript=(
SELECT
Book_NAME AS 'Books.NAME' ,
Book_TypesInfo AS 'Books.TypesInfo' ,
Book_ClassInfo AS 'Books.ClassInfo' ,
REPLACE(REPLACE(Book_MethodSales,'1','租'),'2','買') AS 'Books.Sales'
FROM BooksInfo WITH(NOLOCK)
WHERE
(
Book_MethodSales IN (@_InBox_Search_Method_Lease)
or
Book_MethodSales IN (@_InBox_Search_Method_Sales)
)
FOR JSON PATH,INCLUDE_NULL_VALUES
)
End
SELECT @_InBox_Search_Method_Lease,@_InBox_Search_Method_Sales,@_InBox_Search_BookNAME,@_OutBox_SearchTSQLScript
[查詢結果1]
當傳進的查詢條件:只查有 Lease = 1
DECLARE @_InBox_JSON_Source nvarchar(Max)='[
{
"ProgID":"Books",
"ProgVersion":"1.0",
"ProgAction":"Search",
"ProgDevice":"Sony",
"ProgDeviceOS":"Android",
"ProgDeviceLocale":"Taiwan",
"ProgDeviceHardware":"???",
"ProgServiceCode":"",
"SearchInfo":{
"BookName":null,
"MethodSales":{
"Lease":1,
"Sales":null
}
}
]'
只會取得
[{"Books":{"NAME":"白雪公主","TypesInfo":"童書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"Win10 手冊","TypesInfo":"電腦書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"PCHOME","TypesInfo":"雜誌","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"今周刊","TypesInfo":"財經書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"資產管理","TypesInfo":"其他","ClassInfo":"書","Sales":"租"}}]
[查詢結果2]
當傳進的查詢條件:只查有 Sales = 1
DECLARE @_InBox_JSON_Source nvarchar(Max)='[
{
"ProgID":"Books",
"ProgVersion":"1.0",
"ProgAction":"Search",
"ProgDevice":"Sony",
"ProgDeviceOS":"Android",
"ProgDeviceLocale":"Taiwan",
"ProgDeviceHardware":"???",
"ProgServiceCode":"",
"SearchInfo":{
"BookName":null,
"MethodSales":{
"Lease":null,
"Sales":1
}
}
]'
[{"Books":{"NAME":"獅子王","TypesInfo":"童書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"Office2016","TypesInfo":"電腦書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"園藝","TypesInfo":"生活書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"十字起子","TypesInfo":"工具書","ClassInfo":"書","Sales":"買"}}]
[查詢結果3]
當傳進的查詢條件:只查有 Sales = 1 and Lease = 1
DECLARE @_InBox_JSON_Source nvarchar(Max)='[
{
"ProgID":"Books",
"ProgVersion":"1.0",
"ProgAction":"Search",
"ProgDevice":"Sony",
"ProgDeviceOS":"Android",
"ProgDeviceLocale":"Taiwan",
"ProgDeviceHardware":"???",
"ProgServiceCode":"",
"SearchInfo":{
"BookName":null,
"MethodSales":{
"Lease":1,
"Sales":1
}
}
]'
[{"Books":{"NAME":"白雪公主","TypesInfo":"童書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"Win10 手冊","TypesInfo":"電腦書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"PCHOME","TypesInfo":"雜誌","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"今周刊","TypesInfo":"財經書","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"資產管理","TypesInfo":"其他","ClassInfo":"書","Sales":"租"}},{"Books":{"NAME":"獅子王","TypesInfo":"童書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"Office2016","TypesInfo":"電腦書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"園藝","TypesInfo":"生活書","ClassInfo":"書","Sales":"買"}},{"Books":{"NAME":"十字起子","TypesInfo":"工具書","ClassInfo":"書","Sales":"買"}}]
[查詢結果4]
當傳進的查詢條件: Sales = 1 and Lease = 1 and BookName="PCHOME"
DECLARE @_InBox_JSON_Source nvarchar(Max)='[
{
"ProgID":"Books",
"ProgVersion":"1.0",
"ProgAction":"Search",
"ProgDevice":"Sony",
"ProgDeviceOS":"Android",
"ProgDeviceLocale":"Taiwan",
"ProgDeviceHardware":"???",
"ProgServiceCode":"",
"SearchInfo":{
"BookName":"PCHOME",
"MethodSales":{
"Lease":1,
"Sales":1
}
}
]'
[{"Books":{"NAME":"PCHOME","TypesInfo":"雜誌","ClassInfo":"書","Sales":"租"}}]
語法的部份可以傳入的 JSON 再多種變化,產生最後的 JSON 就傳給前端,不管是 .NET or JQ 都可以解析取得的 JSON 的內容,下達 CREATE PROCEDURE 之後就可以直接下達
EXECUTE [SP名稱] 'JSON' 就會回傳相關資料了