【发布时间】:2012-05-09 11:57:33
【问题描述】:
我有两个表,它们的列相同,但数据不同。它们代表两个不同的日子。这些表是 D1Table 和 D2Table。列是作者 1、作者 2、作者 3、作者 4、作者 5、位置 1 - 位置 5、标题和传记 - 传记 5。在查询中,我试图获取与用户在文本框中键入的文本相匹配的列(这样他们就可以在这两天进行搜索)。
到目前为止,我在第一张桌子上工作得很好,但我真的很想搜索第一张和第二张桌子,我只是不知道怎么做,我尝试过使用 join 和 union 但我没有太多幸运的是我只是得到错误显然我做错了什么。而且我使用的查询真的很长,你可以看到,我相信一定有更好的方法来做到这一点:
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 查询很垃圾(我才刚刚开始学习它们)。我一直在互联网上寻找一段时间试图学习语法,但这是我所掌握的,所以我认为可能是我寻求帮助的时候了。如果有人能给我一些指导,将不胜感激。
【问题讨论】:
标签: c# asp.net sql sql-server-2005