SQL Query从同一个表中搜索按状态过滤

时间:2014-02-03 15:34:02

标签: mysql asp.net sql sql-server

好吧......查询是,
我有一张表:Website_Articles

Article, Id, Author, PublishedOn, ArticleName是用于存储不同页面的数据的公共槽。它通过提供状态ID来区分。 (例如:获得图片)

enter image description here

我想要实现的是使用搜索查询,我想通过提到的状态ID仅从特定列过滤结果数据,或者搜索结果不应来自提到的状态ID。 (两种方式都可以)
我拥有的命令就是这个....

SELECT SUBSTRING(Article, 0, CHARINDEX(' ', Article, 300)) + '...' 
AS Article, Id, Author, PublishedOn, ArticleName 
FROM Website_Articles 
WHERE (Article LIKE '%' + @Article + '%') or 
(ArticleName LIKE '%' + @ArticleName + '%' ) 

但这会带来Website_Articles

的所有内容

非常感谢任何帮助。 谢谢。

1 个答案:

答案 0 :(得分:1)

这可能是你想要的吗?

SELECT SUBSTRING(Article, 0, CHARINDEX(' ', Article, 300)) + '...' 
AS Article, Id, Author, PublishedOn, ArticleName 
FROM Website_Articles 
WHERE Status = 'Menu' AND
  ((Article LIKE '%' + @Article + '%') or 
   (ArticleName LIKE '%' + @ArticleName + '%' ))

SELECT SUBSTRING(Article, 0, CHARINDEX(' ', Article, 300)) + '...' 
AS Article, Id, Author, PublishedOn, ArticleName 
FROM Website_Articles 
WHERE Status <> 'Menu' AND
  ((Article LIKE '%' + @Article + '%') or 
   (ArticleName LIKE '%' + @ArticleName + '%' ))