2012-04-29 81 views
1

列的我有全部具有相同的列,但不同數據的兩個表。他們代表兩個不同的日子。這些表格是D1Table和D2Table。這些列是author1,author2,author3,author4,author5,position1 - position5,title和biography- biography5。在查詢中,我嘗試獲取與用戶輸入文本框中的文本相匹配的列(以便他們可以在兩天內進行搜索)。SQL查詢兩個表很多

我至今工作於第一個表不錯,但我真的想同時搜索第一和第二個表,我只是不知道如何,我曾嘗試使用和加入工會,但我沒有很幸運,我只是得到錯誤,顯然我做錯了什麼。此外,我現在用的查詢是很長的,你可以看到,我肯定必須有一個更好的方式來做到這一點:

txt = "SELECT * from D1Table WHERE Synopsis LIKE '%" + txtBText + 
     "%' OR Author1 LIKE '%" + txtBText + "%' OR Author2 LIKE '%" + txtBText + 
     "%' OR Author3 LIKE '%" + txtBText + "%' OR Author4 LIKE '%" + txtBText + 
     "%' OR Author5 LIKE '%" + txtBText + "%' OR Biography1 LIKE '%" + txtBText + 
     "%' OR Biography2 LIKE '%" + txtBText + "%' OR Biography3 LIKE '%" + txtBText + 
     "%' OR Biography4 LIKE '%" + txtBText + "%' OR Biography5 LIKE '%" + txtBText + 
     "%' OR Title LIKE '%" + txtBText + "%' OR Position1 LIKE '%" + txtBText + 
     "%'OR Position2 LIKE '%" + txtBText + "%' OR Position3 LIKE '%" + txtBText + 
     "%' OR Position4 LIKE '%" + txtBText + "%' OR Position5 LIKE '%" + txtBText + "%' "; 

現在我知道這是很可怕的,正如你也許能告訴我垃圾與SQL查詢(我剛開始學習它們)。我一直在尋找整個互聯網一段時間,試圖學習語法,但這是我所知道的,所以我認爲這可能是我要求幫助的時候。如果任何人都可以給我一些指導,將不勝感激。

回答

2

我的第一反應是,爲什麼你需要這樣的做法,可能是你應該解釋一下您的需求更好。但無論如何,你最好看看full text search,只是谷歌它。

下面是一些有用的鏈接:

http://en.wikipedia.org/wiki/Full_text_search

在文本檢索,全文檢索是指技術搜索 一臺計算機存儲的文檔或全文 數據庫的集合。全文本搜索從搜索基於 元數據或在數據庫 (如標題,摘要,選擇的部分或書目 引用)所表示的原始文本的零件區分開。

在全文搜索,搜索引擎檢查所有的詞語的每 存儲的文檔,因爲它試圖匹配搜索準則(例如,由用戶提供的 字)。全文檢索技術在20世紀90年代變成了在線書目數據庫中常見的 [需要驗證 ]。許多網站和應用程序(如字 處理軟件)提供全文搜索功能。有些網絡 的搜索引擎如AltaVista的使用全文搜索技術 而其 索引系統檢查其他指標只有網頁的一部分。[1]

http://msdn.microsoft.com/en-us/library/ms142571.aspx 從上面的鏈接的一些話: 喜歡的全文本搜索

比較相較於全文檢索,在LIKE的Transact-SQL謂詞工作 僅字符模式。另外,您不能使用LIKE謂詞以 查詢格式化的二進制數據。此外,針對大量非結構化文本數據的LIKE查詢比針對相同數據的等效 全文查詢慢得多。針對數百萬行文本數據的 LIKE查詢可能需要幾分鐘才能返回;而對於相同的數據,全文 查詢可能只需幾秒或更少時間,具體取決於返回的行數爲 。

+0

這看起來很有趣,我會考慮這樣做,感謝您的鏈接。 – OverHere 2012-04-29 17:08:27

1

如果你能,表的重新設計,將真正幫助。 根據多少性能也有,和原因劃分的日子到不同的表,你可以有一個名爲DayData一個表,該表中的列 日期(也許存儲爲int或使用SQL 2008 R2的DATE數據類型) 場 值

如果位置事項(如作者1以某種方式從author2區別對待的),那麼你還可以有一個索引字段(整型)。 這取決於你是否總是具有每種類型的恰好5個值,或者如果你可以具有0,1或N,和類型是否可以隨時間改變。

無論如何,如果你堅持你有什麼,你可以嘗試全文搜索,或者如果這是遙遠的橋,嘗試

"SELECT * from (select * from D1Table UNION D2Table) D1D2 
WHERE Synopsis LIKE '%" + txtBText + "%' OR Author1 LIKE '%" + txtBText + "%' 
OR Author2 LIKE '%" + txtBText + "%' OR Author3 LIKE '%" + txtBText + "%' 
OR Author4 LIKE '%" + txtBText + "%' OR Author5 LIKE '%" + txtBText + "%' 
OR Biography1 LIKE '%" + txtBText + "%' OR Biography2 LIKE '%" + txtBText + "%' 
OR Biography3 LIKE '%" + txtBText + "%' OR Biography4 LIKE '%" + txtBText + "%' 
OR Biography5 LIKE '%" + txtBText + "%' OR Title LIKE '%" + txtBText + "%' 
OR Position1 LIKE '%" + txtBText + "%'OR Position2 LIKE '%" + txtBText + "%' 
OR Position3 LIKE '%" + txtBText + "%' OR Position4 LIKE '%" + txtBText + "%' 
OR Position5 LIKE '%" + txtBText + "%' "; 

注稱爲D1D2派生表:(選擇*從D1Table UNION D2Table)

但是,你有另一個問題 - 建立聲明好像你是很容易受到SQL注入攻擊(搜索本網站或谷歌)。有人可能會把

'--delete * from D1Table; 'select * from D1Table where ''1= 

並刪除您的D1Table中的數據。各大網站開始使用遭到黑客攻擊類似的技術 - 而不是刪除查詢操作產生的錯誤信息或故意拖延時間的數據,泄露有關數據庫設計和用戶名,密碼等

希望幫助:)

+0

我可能會重新設計表格。我不知道我爲什麼把它們分開,我顯然沒有想到它。我會看看SQL注入的東西,我想我需要更多地瞭解這一點。一個簡單的問題,當我使用HttpUtility.HtmlEncode()取文本框的值時, 所以我使用的字符串是:字符串txtBText = HttpUtility.HtmlEncode(TextBoxSearch.Text);;我想知道是不是真的不夠防止來自用戶輸入的SQL攻擊? (我假設沒有) – OverHere 2012-04-29 17:11:54

+0

啊我看到我已經閱讀了一些關於SQL注入的內容,我應該使用參數,所以我會研究這個。 – OverHere 2012-04-29 17:32:27

+0

@Aaron Bertrand - 感謝您對格式化的整理。我還是習慣了StackOverflow :)(順便說一下你博客的粉絲!) – 2012-05-04 02:03:52

0
信息

雖然我基本上是我在這裏看到其他的答案同意,我會採取不同的策略對我的回答:

如果你正在努力學習SQL,那麼它是你必須訓練自己使用更好formmating。這可能看起來小,但說實話,你會發現很容易閱讀,理解和特別編輯,如果你使用更容易理解的格式。

雖然我真的很想念C/C++類型的塊引號在C#,VB數十年的編程已經適應我的這個有點醜陋的要求沒有他們。比較你的例子:

TXT = 「SELECT * FROM D1Table WHERE簡介LIKE「%」 + txtBText + 「% '或作者1 LIKE' %」 + txtBText + 「% '或Author2 LIKE' %」 + txtBText + 「%」OR Author3 LIKE'%'+ txtBText +「%」OR Author4 LIKE'%「+ txtBText +」%「OR Author5 LIKE'%」+ txtBText +「%」OR Biography1 LIKE'%「+ txtBText +」% 'OR'Biography2 LIKE'%「+ txtBText +」%「OR Biography3 LIKE'%」+ txtBText +「%」OR Biography4 LIKE'%「+ txtBText +」%「OR Biography5 LIKE'%」+ txtBText +「%'OR標題LIKE'%'+ txtBText +「%」OR Position1 LIKE'%「+ txtBText +」%「OR Position2 LIKE'%」+ txtBText +「%」OR Position3 LIKE'%「+ txtBText +」%「OR Position4 LIKE '%'+ txtBText +「%'OR Position5 LIKE'%」+ txtBText +「%'」;

爲它的等效:

txt = "" 
+ " SELECT * " 
+ " from D1Table " 
+ " WHERE Synopsis LIKE '%" + txtBText + "%' " 
+ "  OR Author1  LIKE '%" + txtBText + "%' " 
+ "  OR Author2  LIKE '%" + txtBText + "%' " 
+ "  OR Author3  LIKE '%" + txtBText + "%' " 
+ "  OR Author4  LIKE '%" + txtBText + "%' " 
+ "  OR Author5  LIKE '%" + txtBText + "%' " 
+ "  OR Biography1 LIKE '%" + txtBText + "%' " 
+ "  OR Biography2 LIKE '%" + txtBText + "%' " 
+ "  OR Biography3 LIKE '%" + txtBText + "%' " 
+ "  OR Biography4 LIKE '%" + txtBText + "%' " 
+ "  OR Biography5 LIKE '%" + txtBText + "%' " 
+ "  OR Title  LIKE '%" + txtBText + "%' " 
+ "  OR Position1 LIKE '%" + txtBText + "%' " 
+ "  OR Position2 LIKE '%" + txtBText + "%' " 
+ "  OR Position3 LIKE '%" + txtBText + "%' " 
+ "  OR Position4 LIKE '%" + txtBText + "%' " 
+ "  OR Position5 LIKE '%" + txtBText + "%' " 
; 

顯然更易於閱讀和更容易理解。什麼是不明顯的是,它也更容易編輯。想想看,你需要通過添加UNION檢查兩個表:

txt = "" 
// <CUT from here 
+ " SELECT * " 
+ " from D1Table " 
+ " WHERE Synopsis LIKE '%" + txtBText + "%' " 
+ "  OR Author1  LIKE '%" + txtBText + "%' " 
+ "  OR Author2  LIKE '%" + txtBText + "%' " 
+ "  OR Author3  LIKE '%" + txtBText + "%' " 
+ "  OR Author4  LIKE '%" + txtBText + "%' " 
+ "  OR Author5  LIKE '%" + txtBText + "%' " 
+ "  OR Biography1 LIKE '%" + txtBText + "%' " 
+ "  OR Biography2 LIKE '%" + txtBText + "%' " 
+ "  OR Biography3 LIKE '%" + txtBText + "%' " 
+ "  OR Biography4 LIKE '%" + txtBText + "%' " 
+ "  OR Biography5 LIKE '%" + txtBText + "%' " 
+ "  OR Title  LIKE '%" + txtBText + "%' " 
+ "  OR Position1 LIKE '%" + txtBText + "%' " 
+ "  OR Position2 LIKE '%" + txtBText + "%' " 
+ "  OR Position3 LIKE '%" + txtBText + "%' " 
+ "  OR Position4 LIKE '%" + txtBText + "%' " 
+ "  OR Position5 LIKE '%" + txtBText + "%' " 
// CUT to here> 
// VV add a UNION 
+ " UNION ALL " 
// <PASTE here 
+ " SELECT + " 
+ " from D2Table " // <-- change the table name here 
+ " WHERE Synopsis LIKE '%" + txtBText + "%' " 
+ "  OR Author1  LIKE '%" + txtBText + "%' " 
+ "  OR Author2  LIKE '%" + txtBText + "%' " 
+ "  OR Author3  LIKE '%" + txtBText + "%' " 
+ "  OR Author4  LIKE '%" + txtBText + "%' " 
+ "  OR Author5  LIKE '%" + txtBText + "%' " 
+ "  OR Biography1 LIKE '%" + txtBText + "%' " 
+ "  OR Biography2 LIKE '%" + txtBText + "%' " 
+ "  OR Biography3 LIKE '%" + txtBText + "%' " 
+ "  OR Biography4 LIKE '%" + txtBText + "%' " 
+ "  OR Biography5 LIKE '%" + txtBText + "%' " 
+ "  OR Title  LIKE '%" + txtBText + "%' " 
+ "  OR Position1 LIKE '%" + txtBText + "%' " 
+ "  OR Position2 LIKE '%" + txtBText + "%' " 
+ "  OR Position3 LIKE '%" + txtBText + "%' " 
+ "  OR Position4 LIKE '%" + txtBText + "%' " 
+ "  OR Position5 LIKE '%" + txtBText + "%' " 
// ^^ change these column names if needed 
; 

剩下的是什麼離開這裏是一個聯盟,SELECT的必須返回相同的列數和數據類型的列必須是兼容。因此,如果D1Table和D2Table具有不同的列,那麼您必須刪除「*」併爲每個SELECT創建兼容的列表。

+0

謝謝,看起來比我所看到的更容易閱讀。謝謝(你的)信息。 – OverHere 2012-04-29 17:10:40

+0

這絕對是寫作的方式(格式化 - 不確定設計仍然存在)如果我親自使用了我在我的答案中快速放在一起的片段,並且在我的團隊中有人使用它。 – 2012-05-04 02:06:10

0

您可以使用UNION ALL並運行兩個查詢得到的結果......如通過RBarryYoung

SELECT COLUMN1, COLUMN2 FROM TABLE1 WHERE COLUMN1 LIKE '% Variable %' OR COLUMN2 LIKE '% Variable %' 
UNION ALL 
SELECT COLUMN1, COLUMN2 FROM TABLE2 WHERE COLUMN1 LIKE '% Variable %' OR COLUMN2 LIKE '% Variable %'