iT邦幫忙

1

SQL要怎麼下?

XYZ 2020-05-06 14:44:213898 瀏覽
  • 分享至 

  • xImage

若開放使用者查one name、two name、three name的欄位,有時使用者只查租或只查買或租和買都查SQL要怎麼下?

租和買的two name、three name的代號有的相同但是不同的東西
若開放使用者查one name、two name、three name的欄位,有時使用者只查租或只查買或租和買都查SQL要怎麼下?

請問SQL 可以下判斷式若是租的話執行SQL1,若是買的話執行SQL2,若租及買都查執行SQL3?

https://ithelp.ithome.com.tw/upload/images/20200506/20108157N9qTCvuS9o.jpg

where條件用one=你設定的值,同時查詢可以用in('1','2')這樣的方式,如果你是用網頁或是程式給人操作的話,可以用下拉選單或是其他選擇的方式,在依照使用者選擇的選項去處理
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
ysy2019
iT邦見習生 ‧ 2020-05-11 14:48:52
最佳解答

假設資料在 #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 ) ) 
2
通靈亡
iT邦高手 1 級 ‧ 2020-05-06 15:35:42
SELECT *
FROM table
WHERE one IN (1,2) AND -- 只租: (1),只買: (2),租或買:(1,2)
      two = two的代號 AND
      three = three的代號
XYZ iT邦新手 4 級 ‧ 2020-05-06 15:50:20 檢舉

有時只查租的話, 用in會把買的資料也帶入

通靈亡 iT邦高手 1 級 ‧ 2020-05-06 15:53:08 檢舉

你「有時只查租」前端怎麼帶到後端的?

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-05-06 15:37:17
//只查租
SELECT * FROM TABLENAME WHERE one_name="租"
//只查買
SELECT * FROM TABLENAME WHERE one_name="買"
//查租或買
SELECT * FROM TABLENAME WHERE one_name="租" OR  one_name="買"

另外有個小小建議
one, two, three 三個欄位都可以取消不要

1
rogeryao
iT邦超人 7 級 ‧ 2020-05-06 17:18:24

這個 Table 好像怪怪的,第 4 行

1	租	1	書	3	雜誌	PC HOME

若也可以"買",不就變成

2	買	1	書	3	雜誌	PC HOME

第 9 行

2	買	1	書	3	生活書	園藝

那麼 2,1,3 是要對應到 "雜誌 PC HOME" 還是 "生活書 園藝" ?

通靈亡 iT邦高手 1 級 ‧ 2020-05-06 18:42:53 檢舉

個人猜測
one 對 one name
two 對 two name
three 對 three name
item name 是名稱

我發現他發問都喜歡讓別人通靈他的問題
如果哪天他當上PM,下面的工程師遲早會被他氣死

rogeryao iT邦超人 7 級 ‧ 2020-05-06 23:40:34 檢舉

以下範例純屬臆測
Demo

同感,而且我覺得大家都好友善啊!怎麼會有力氣去參透他的問題呢? XD

0
Pyan
iT邦研究生 5 級 ‧ 2020-05-06 17:38:50

先把租和買分兩個子查詢呢

0
微笑
iT邦研究生 5 級 ‧ 2020-05-07 09:00:53

請問SQL 可以下判斷式若是租的話執行SQL1,若是買的話執行SQL2,若租及買都查執行SQL3?

這部份不歸SQL管,SQL負責新增&刪除&修改&查詢你的資料庫
三個獨立的SQL就是三個獨立的SQL,判斷要執行哪一個SQL的是在他之外的東西,這東西通常叫做後端

然後叫後端作業的人(也就是要資料)是前端,所以這邊判斷你的問題點也許不在SQL
而是對基本的介面不瞭解

這部份的問題不訪根據你使用的語言發問,且如果沒有自信說明完整的話,可以在不透漏隱私的程度下提供程式碼,這樣就不會因為看不懂別人給的答案,結果自己問的也不是這個問題導致浪費時間喔!/images/emoticon/emoticon37.gif

1
純真的人
iT邦大師 1 級 ‧ 2020-05-07 18:07:52

如果都只在資料庫的話..

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

0
PPTaiwan
iT邦好手 1 級 ‧ 2020-05-07 20:19:31

[資料內容]
https://ithelp.ithome.com.tw/upload/images/20200507/20104851b90uUBJplS.png

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
				

https://ithelp.ithome.com.tw/upload/images/20200507/20104851SMWkUhL9TT.png

[查詢結果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' 就會回傳相關資料了

我要發表回答

立即登入回答