English 中文(简体)
左翼左翼加入军装
原标题:mysql left join ascending

我要问:

   SELECT reply.id,
          reply.message,
          reply.userid,
          reply.date,
          medal.id,
          medal.url,
          medal.name,
          user.id,
          user.name AS username
     FROM posts AS reply
LEFT JOIN users AS user ON reply.userid = user.id
LEFT JOIN medals AS medal ON medal.userid = user.id
 GROUP BY reply.id
 ORDER BY reply.id ASC

everything is OK, except that I get the medal ascending not descending which mean that it grab the first medal that user got - I need to get the last one.

问题回答

看到每个群体第一个记录是意外的。 按组别选择一个非组类、非组别一栏,将产生一个未界定的记录。

详细解释如下:。 http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html

你重新做些什么的正确方法是使用一个子座,在座标上,你选择每个理想群体的最高潮日。

本文概述了这一办法:

也许,你可以为奖牌提供分局或临时桌,而不是直接加入奖牌。 临时表格是粉碎表,但分类为DESC。 排序为LEFT JOIN(通过电离层、尿液、按 des子顺序排列的名字),但我感到,这样做非常缓慢,不是最佳选择。

最简单的解决办法是:

ORDER BY reply.id ASC, medal.id DESC

www.un.org/chinese/ga/president

你们可以过滤后有多个奖牌的岗位/用户(含有你不希望的奖牌的跳板)。

下一个选择是,在分局选择<代码>(中等),然后加入。 这令人迷惑,但却是站不住脚的。 这里是一般的想法(可能必须重新命名几个桌子,因为它们不止一次使用):

SELECT *,
       medal.url,
       medal.name
FROM
(
   SELECT reply.id,
          reply.message,
          reply.userid,
          reply.date,
          MAX(medal.id) AS medal_id,
          user.id,
          user.name AS username
     FROM posts AS reply
LEFT JOIN users AS user ON reply.userid = user.id
LEFT JOIN medals AS medal ON medal.userid = user.id
 GROUP BY reply.id
 ORDER BY reply.id ASC
)
AS t
LEFT JOIN medals AS medal ON medal.id = t.medal_id




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签