如何从结果查询中排除第一行?

时间:2014-09-28 14:04:55

标签: php mysql sql

考虑以下查询 -

SELECT * FROM GAME_LIST WHERE
  genre     = (SELECT Genre FROM GAME_LIST WHERE name='$searchTerm') OR
  publisher = (SELECT Publisher FROM GAME_LIST WHERE name='$searchTerm')
  ORDER BY CASE WHEN name LIKE '%$searchTerm%' then 1 else 2 end, points DESC

此查询将从GAME_LIST表生成多个行。例如 -

A - 123 - W
B - 235 - R
C - 323 - R
D - 146 - R
E - 167 - W
F - 189 - W
G - 165 - R

但我不想包括第一行(在本例中为A - 123 - W) 如何在现有查询中实现此目的?

2 个答案:

答案 0 :(得分:1)

使用offset的{​​{1}}功能。在MySQL中,这通常写成:

limit

第二个参数只是一个很大的数字,以确保您获得所需的所有行。

这也可以写成:

  ORDER BY CASE WHEN name LIKE '%$searchTerm%' then 1 else 2 end, points DESC
  LIMIT 1, 9999999

LIMIT 99999999 OFFSET 1 计数从第一行的OFFSET开始,因此0会跳过第一行,从第二行开始。

答案 1 :(得分:0)

使用以下内容:

SELECT * FROM GAME_LIST WHERE
  genre     = (SELECT Genre FROM GAME_LIST WHERE name='$searchTerm') OR
  publisher = (SELECT Publisher FROM GAME_LIST WHERE name='$searchTerm')
  ORDER BY CASE WHEN name LIKE '%$searchTerm%' then 1 else 2 end, points DESC 
  LIMIT 2, (    SELECT * FROM GAME_LIST WHERE
     genre     = (SELECT Genre FROM GAME_LIST WHERE name='$searchTerm') OR
     publisher = (SELECT Publisher FROM GAME_LIST WHERE name='$searchTerm'))