使用INNER JOIN进行SQL分页并使用LIKE进行过滤

时间:2014-07-22 18:49:13

标签: sql join pagination sql-like

此查询通过排序,过滤和分页来提供数据表。所有功能都正常工作,直到我添加了INNER JOIN,然后我得到了:

The multi-part 'identifier "Types.Description" could not be bound

如果我在查询结尾处删除第二个WHERE子句,LIKE语句可以正常工作,但我会失去分页。我删除了一些LIKE子句以尝试清除这个可怕的查询。

SELECT * 
 FROM ( 
    SELECT ROW_NUMBER() OVER (ORDER BY  TAG asc) AS RowNumber, * 
    FROM (
      SELECT (SELECT COUNT(*) FROM Instruments) AS TotalDisplayRows, (SELECT COUNT(*) FROM Instruments) AS TotalRows, Instruments.Tag, Instruments.Location, Instruments.Description, Types.Description As TypeDesc, Manufacturer.Name, Lease.Name as LeaseName, Facility.Name as FacName 
      FROM Instruments 
      INNER JOIN Types ON Instruments.Type = Types.ID 
      INNER JOIN Manufacturer ON Instruments.Manufacturer = Manufacturer.ID 
      INNER JOIN Facility ON Instruments.Facility = Facility.ID 
      INNER JOIN Lease ON Instruments.Lease = Lease.ID 
      WHERE (Types.Description LIKE '%Cat%')
    ) RawResults
  ) Results 
  WHERE (Types.Description LIKE '%Cat%') AND RowNumber BETWEEN 1 AND 10

1 个答案:

答案 0 :(得分:0)

我认为这是你的问题

WHERE (types.description LIKE '%Cat%') 

您无法执行此操作,因为您实际上是从名为Results的派生表中进行选择,并且您将该列别名为TypeDesc

所以它应该是

WHERE (results.typeDesc LIKE '%Cat%')