2011-01-24 209 views
0

這是我在這個論壇上的第一篇文章,我當然希望得到大師:)一些幫助比賽SQL列

什麼,我想實現的是,有我在db表7列。一個是主鍵,其餘6個是整數列。現在我的應用程序將允許用戶輸入6個值。我想比較6個值由用戶輸入這6個整數列,如果在任何行3列或更多列匹配用戶的輸入主鍵的表返回..

我已經嘗試使用查詢,遊標,嵌套循環和我仍然無法實現我的結果..

任何幫助,將不勝感激!

感謝

修訂

數據庫表結構..我輸入2,3,7,11,45,65。返回的行數應該是5.如..

UID A1 A2 A3 A4 A5 A6 
----------------------- 
1 2 3 4 5 6 7 
2 2 3 4 55 56 57 
3 65 11 45 66 67 68 
4 45 7 11 99 98 97 
5 7 7 7 7 7 7 
6 7 7 7 7 7 7 
7 8 8 8 8 8 8 
8 8 8 8 8 8 8 
9 45 45 0 3 1 2 
10 65 7 4 0 0 0 

返回的ID應該是1,3,4,9,10

+0

你是如何生成主鍵的? – 2011-01-24 14:34:24

+0

重要問題:您是否嘗試將輸入與相應列(組合鎖)匹配,或者任何輸入是否與任何列匹配?換句話說,1,2,3,4,5,6是否匹配6,5,4,7,7,7三次或零次? 此外,是允許重複的號碼還是這更像是一個基諾問題? – nycdan 2011-01-24 15:08:56

+0

任何輸入都可以與數據庫中的任何列匹配.. – mumair 2011-01-24 15:40:38

回答

1

你可以通過迭代值,並添加的變量符合比較條件的每一列。

像這樣:

DECLARE @Accumulator int 
DECLARE @Threshold int 
DECLARE @ReturnValue int 

DECLARE @PK int 
DECLARE @Col1 int 
DECLARE @Col2 int 
DECLARE @Col3 int 
DECLARE @Col4 int 
DECLARE @Col5 int 
DECLARE @Col6 int 

DECLARE @UserInput1 int 
DECLARE @UserInput2 int 
DECLARE @UserInput3 int 
DECLARE @UserInput4 int 
DECLARE @UserInput5 int 
DECLARE @UserInput6 int 

-- Assuming you would set each @UserInputx above to a value input by the user... 

SET @Accumulator = 0 
SET @Threshold = 3 
SET @ReturnValue = 0 

SELECT 
    @PK = PrimaryKey, 
    @Col1 = Col1, 
    @Col2 = Col2, 
    @Col3 = Col3, 
    @Col4 = Col4, 
    @Col5 = Col5, 
    @Col6 = Col6 
FROM TheTable 

IF @Col1 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Col2 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Col3 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Col4 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Col5 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Col6 IN (@UserInput1, @UserInput2, @UserInput3, @UserInput4, @UserInput5, @UserInput6) 
    SET @Accumulator = @Accumulator + 1 

IF @Accumulator >= 3 
    SET @ReturnValue = @PK 
2

啞數據(從MSDN PIVOT例子)

CREATE TABLE Mytable (PK int, col1 int, col2 int, col3 int, col4 int, col5 int, col6 int) 
GO 
INSERT INTO Mytable VALUES (1, 1,4,3,5,4,4) 
INSERT INTO Mytable VALUES (2, 2,4,1,5,5,5) 
INSERT INTO Mytable VALUES (3, 3,4,3,5,4,4) 
INSERT INTO Mytable VALUES (4, 4,4,2,5,5,4) 
INSERT INTO Mytable VALUES (5, 5,5,1,5,5,5) 
GO 

輸入數據

DECLARE @MyInput TABLE (InputValue int) 
INSERT @MyInput VALUES (2) 
INSERT @MyInput VALUES (4) 
INSERT @MyInput VALUES (5) 

UNPIVOT源列數據到行。現在,我們可以加入和彙總發現,我們有3根沒有大規模的火柴或條款

SELECT 
    * 
FROM 
    myTable MT2 --effectively PIVOT back to get original rows 
    JOIN 
    (
    SELECT 
     PK 
    FROM --7 columns into 3, make column name a row identifier 
     (SELECT PK, col1, col2, col3, col4, col5, col6 FROM myTable) MT 
     UNPIVOT 
     (colvalue FOR RowNumber IN (col1, col2, col3, col4, col5, col6)) cols 
     JOIN --match to user input, set based 
     @MyInput MD ON cols.colvalue = MD.InputValue 
    GROUP BY 
     PK --per original row 
    HAVING 
     COUNT(DISTINCT MD.InputValue) >= 3 
    ) foo ON MT2.PK = foo.PK 

這適用於套2,4,5和1,4,5

0

我認爲這將是最容易實現

SELECT ID 

from TABLE 

WHERE 

CASE WHEN integerCol1 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END + 
CASE WHEN integerCol2 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END + 
CASE WHEN integerCol3 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END + 
CASE WHEN integerCol4 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END + 
CASE WHEN integerCol5 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END + 
CASE WHEN integerCol6 IN 
(@user1,@user2,@user3,@user4,@user5,@user6) THEN 1 ELSE 0 END >=3 
0

我沒有時間寫代碼,但是你提到你嘗試了一個嵌套循環。現在我明白你想要做什麼了,我可以給你一個我認爲對你有用的方向。

在您的內部循環中,您將獲取要比較輸入的第一個列表,然後獲取第一個輸入並將其與列表中的每個成員進行比較,直到達到匹配或結束。現在這個棘手的部分......因爲你不想將第二個輸入數字與列表中的同一個成員匹配兩次(例如,如果你輸入2 7秒),你應該將你匹配的成員更改爲某些非法數字。如果只允許正數,則將其更改爲-1。這確保你不會與它相匹配兩次。

現在在我們的中間循環中,通過其餘的輸入數字對齊同一個列表,直到完成。爲了確定一個匹配,計算該列表中-1的出現次數(或任何用來表示匹配的次數),並且您將獲得成功/失敗指示符。

在您的外部循環中,以相同的方式遍歷每個列表。

在非常粗糙的僞代碼:

var x = set of all lists 
foreach (list l in x) //go through each list 
{ 
foreach (item i in inputlist) //go through each item in your input 
    { 
    foreach (item j in l) //compare to each item in the current list 
     { 
     if (i = j) 
      { 
       j = -1; //set matched item to -1 to avoid rematching 
       sum[l]++; 
      } 
     } 
    } 
} 

完成時,任何列表,其中總和[1]> = 3匹配。

0

好感謝大家的響應,但既不是它幫助我:P

嵌套循環都要經過數據集或gridview的任何適合你..

我把它的辦法解決是連接一排的所有列的值都包括一些特殊字符..就像一行有1 2 3 4 5 6值..它變成[1] [2] [3] [4] [5] [6]然後使用.Contains函數比較值,如果發現use.Replace排除它的字符串,確保當循環遞增時不計算它。

一個經典的方法..但它是值得的!

Thanks guys :)