2017-03-28 86 views
0

我正在使用現有的.net系統,並且我在兩個用戶之間創建了一個帶有「提供」的SQL表。 「offer」從usertype1到usertype2雙向(giggidy),反之亦然。爲了使其「簡單」,有一個「offertype」字段,它定義了要約的方式,以便正確的用戶「接受」要約。如果想要查詢單個商品的數據,可以非常簡單地執行查詢。這是我的問題:選擇具有多個條件條件的語句

我試圖構建一個視圖,顯示user1的所有優惠的列表。這種觀點認爲,應該顯示「offertype1」下用戶1是「offerloginuser」的所有優惠和「offertype2」的所有優惠,其中user1的是「offerrecipientuser」

offerid offerloginuserid offerrecipientuserid offertype 
1  1     2      1 
2  2     1      2 
3  1     3      1 
4  3     1      2 
5  3     4      2 

我需要能夠看到OFFERID的:1 ; 2; 3; 4但不是5,因爲user1不參與。初看起來,它看起來很簡單,你有用戶ID,事情是,系統的設計者選擇使用基於usertype的用戶ID單獨的表,所以我可能有兩個具有相同ID的用戶。

我有兩個選擇語句:

select * from tbl_offers where offertype = 1 and offerloginuserid = 1 

select * from tbl_offers where offertype = 2 and offerrecipientid = 1 

每個單獨執行給我我需要什麼,是否有可能加入他們的行列,並顯示所有結果或做我必須做的加入?我將這些結果綁定到DevExpress網格,以便在登錄時向用戶顯示。

+0

'UNION ALL'可能會有幫助。 – HoneyBadger

回答

4

這可以幫助您嗎?

select * from tbl_offers where (offertype = 1 and offerloginuserid = 1) OR (offertype = 2 and offerrecipientid = 1) 
1

請試試這個: -

select * from tbl_offers 
where offertype in (1,2) and offerrecipientid = 1 
+0

感謝Anup,但您忘記了它是針對一種報價提供offerrecipientid的,另一種類型是offerloginuserid。 @etsa回答了這個問題,我只是在等待,以便我能接受它。 – Daniel