0

我有一个名为的表technical,我可以在哪里放置WHERE我的搜索功能的子句?

我想插入WHERE t1.blklot LIKE '01/01'- 此行将仅显示那些blklot = 01/01

SELECT * 
FROM technical t1
JOIN (
    SELECT blklot, MAX(date_update) AS MAXDATE
    FROM technical
    GROUP BY blklot
) t2 ON t1.blklot = t2.blklot
AND t1.date_update = t2.MAXDATE
ORDER BY t1.blklot
4

2 回答 2

1

您实际上可以将它放在子查询或外部查询中。在子查询中,您需要更改条件中的表别名。您的条件位于用于聚合的键上,因此它可以在任何地方工作。

或者,您可以将其放在on子句中:

ON t1.blklot = t2.blklot and
   t1.date_update = t2.MAXDATE and
   t1.blklot LIKE '01/01'

编辑:

实际上,把它放在子查询中。这是最有效的方法:

SELECT * 
FROM technical t1 JOIN
     (SELECT blklot, MAX(date_update) AS MAXDATE
      FROM technical
      WHERE blklot LIKE '01/01'
     ) t2
     ON t1.blklot = t2.blklot AND
        t1.date_update = t2.MAXDATE
ORDER BY t1.blklot;

因为您只选择一个值,所以group by没有必要。

于 2014-02-01T15:38:15.720 回答
0

像这样试试

SELECT * 
FROM technical t1
  JOIN (
      SELECT blklot, MAX(date_update) AS MAXDATE
      FROM technical
      GROUP BY blklot
      )t2 ON t1.blklot = t2.blklot
      AND t1.date_update = t2.MAXDATE
WHERE t1.blklot LIKE '01/01
ORDER BY t1.blklot

或者在 ON CLAUSE 中做一个条件

SELECT * 
FROM technical t1
  JOIN (
      SELECT blklot, MAX(date_update) AS MAXDATE
      FROM technical
      GROUP BY blklot
      )t2 ON t1.blklot = t2.blklot
      AND t1.date_update = t2.MAXDATE
      AND t1.blklot LIKE '01/01
ORDER BY t1.blklot
于 2014-02-01T15:39:49.643 回答