2012-04-29 82 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 %'