简体   繁体   English

MySQL子查询问题

[英]MySQL sub-query issue

SELECT * 
FROM (SELECT *
  FROM content
  WHERE topic='$id' AND active='1'
  ORDER BY date DESC, id DESC
  LIMIT 4) a
ORDER BY a.likes DESC

I have this query. 我有这个查询。 I want it to select 4 entries from content table, and sort entries as follows: 我希望它从content表中选择4个条目,并对条目进行如下排序:

  1. SELECT most recent entries. 选择最新条目。 It means ORDER BY date(mysql datetime) DESC, id DESC. 它表示ORDER BY date(mysql datetime)DESC,ID DESC。

  2. From those 4 selected, order them by likes(mysql INT) DESC 从这4个中选择,按likes(mysql INT)DESC排序

When runing my query, it returns wrong results. 运行查询时,它返回错误的结果。 It selects entries matching this criteria WHERE topic='$id' AND active='1' . 它选择符合此条件的条目WHERE topic='$id' AND active='1' It sorts entries by likes DESC, but it ignore this criteria ORDER BY date DESC, id DESC , so it shows me results with a smaller id first. 它按喜欢的DESC排序条目,但忽略此条件ORDER BY date DESC, id DESC ,因此它首先显示ID较小的结果。

What can be the reason? 可能是什么原因? Thank you in advance 先感谢您

After OP edits, the correct query will be OP编辑后,正确的查询将是

SELECT * 
FROM (SELECT *
  FROM content
  WHERE topic='$id' AND active='1'
  ORDER BY date DESC, id DESC
  LIMIT 4) a
ORDER BY a.likes DESC, date DESC, id DESC

The ORDER BY on the outermost query specifies the order of the rows returned. 最外面的查询上的ORDER BY指定返回的行的顺序。 No other order of rows is guaranteed or implied. 没有其他行的顺序可以保证或暗示。

From the original question (prior to the edit) sounds like OP wanted the rows returned in descending order by the integer value of the likes column. 从最初的问题(在编辑之前)听起来,OP希望行按likes列的整数值降序返回。 That is, OP wanted to specify: 也就是说,OP要指定:

ORDER BY a.likes DESC

on the outermost query. 在最外面的查询上。


The rows returned by the query will be returned in the sequence defined by ORDER BY on the outermost query. 查询返回的行将按最外层查询的ORDER BY定义的顺序返回。 No other sequencing of rows is guaranteed. 没有其他行排序的保证。

If OP wants the rows returned in a specific order, then the list of expressions in the ORDER BY clause on the outermost query will need to be specified differently. 如果OP希望按特定顺序返回行,则最外层查询的ORDER BY子句中的表达式列表将需要不同地指定。 For example: 例如:

ORDER BY a.likes DESC, a.date DESC, a.id DESC

--or-- - 要么 -

ORDER BY a.date DESC, a.likes DESC, a.id DESC

The ORDER BY in the inline view will be honored by the inline view query; 内联视图查询将使用内联视图中的ORDER BY but once that inline view query is materialized, and is referenced as a row source by the outer query, that ORDER BY is gone. 但是一旦实现了内联视图查询,并被外部查询引用为行源,则ORDER BY就消失了。 The outer query is free to access and return the rows from the inline view (derived table) in any order it wants; 外部查询可以自由访问并以所需顺序从内联视图(派生表)返回行; the outer query isn't required to honor the ORDER BY on the inline view query; 不需要外部查询来接受内联视图查询上的ORDER BY; the outer query just sees the derived table as a row set, like any other table. 外部查询只是将派生表视为行集,就像其他任何表一样。

(This is assuming that " likes " is a column in the content table, and not a result derived from some other table. We don't see what columns your query is returning, because you are using * as the SELECT list.) (这是假设“ likes ”是content表中的一列,而不是从其他表派生的结果。由于您使用*作为SELECT列表,因此我们看不到查询返回的列。)

(If that isn't what OP is looking for, OP can elaborate on the requirements for the specified resultset. Everything else looks right in OP query, for getting the four "latest" rows within the inline view.) (如果这不是OP所需要的,OP可以详细说明指定结果集的要求。OP查询中的其他内容看起来都很正确,以便在嵌入式视图中获取四个“最新”行。)

Try this:
SELECT *
  FROM content
  WHERE topic='$id' AND active='1'
  ORDER BY date DESC, likes desc
  LIMIT 4

You dont need another level of select to do order by.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM