在与MySQL相同的表中排除第二个查询的第一个查询的结果

时间:2012-03-06 20:22:07

标签: mysql

我想这样做:

SELECT * FROM `langCategories` ORDER BY `name` ASC WHERE ORDER BY `amount` DESC LIMIT 8,0

但这不可能。

我的网站中有2个部分: 的 1。前8个人口类别

(SELECT * FROM `langCategories` ORDER BY `langCategories`.`amount` DESC LIMIT 0,8)

2。类别的其余部分按名称排序(不包括前8名) (???)

我用php做到了:

$var = db_multiselect("SELECT * FROM `langCategories` ORDER BY `amount` DESC LIMIT 0,8");
$var2 = db_multiselect("SELECT * FROM `langCategories` ORDER BY `name` ASC");

$i=-1;
while ($row = mysqli_fetch_array($var)) // Save the top8 categories
{
   ++$i;
   $top8[$i] = $row["name"];
}

$i=0;
while ($row2 = mysqli_fetch_array($var2))
{
   if (!(in_array($row2["name"], $top8))) // compare the rest of categories excluding top 8
   {
      ...
   }
}

但我想用MySQL查询做到这一点,怎么做呢?

2 个答案:

答案 0 :(得分:1)

子查询!

SELECT * FROM (
  SELECT * FROM `langCategories` 
  ORDER BY `langCategories`.`amount` DESC 
  LIMIT 8,2000000000
) AS baseview 
ORDER BY name ASC

按字母顺序为您提供NOT前8个类别

答案 1 :(得分:0)

这应该适用于MySQL(我希望):

SELECT *
FROM (
  SELECT lc.*, @rownum:=@rownum+1 AS rownum
  FROM `langCategories` lc, (SELECT @rownum:=0) r
  ORDER BY `amount` DESC
)
ORDER BY CASE WHEN rownum <= 8 THEN rownum ELSE 999 END, name