Mysql JOIN查询最大值

问题描述:

这是我遇到的问题的示例。该查询应返回行paulrick,因为它们对子行具有最高的评级。相反,查询返回daveowen,我的猜测是因为它们是第一个子行。我正在分组position和使用MAX(child.rating),但查询不像我想要的那样工作。在真正的表中,我有很多列,这就是为什么我在select子句中使用child.*Mysql JOIN查询最大值

MYTABLE

id | name | parentid| position| rating | 
    1 | mike | 1 | 1  | 6 | 
    2 | dave | 1 | 2  | 5 | 
    3 | paul | 1 | 2  | 7 | 
    4 | john | 1 | 2  | 3 | 
    5 | mike | 5 | 1  | 8 | 
    6 | owen | 5 | 2  | 2 | 
    7 | rick | 5 | 2  | 9 | 
    8 | jaye | 5 | 2  | 3 | 



$getquery = mysql_query("SELECT MAX(child.rating),child.* FROM mytable child 
     LEFT JOIN mytable parent on parent.parentid=child.parentid 
     WHERE parent.name LIKE '%mike%' GROUP BY child.position,child.parentid"); 



while($row=mysql_fetch_assoc($getquery)) { 

$id = $row['id']; 
$name = $row['name']; 
$parentid = $row['parentid']; 



if($id==$parentid) { 
    continue; 
    } 
echo "<p>Name: $name </p>"; 

} 

有趣的事情,我刚刚意识到你在找什么。下面是最终的查询:

select t1.* from mytable t1 
left join mytable t2 
on t1.parentid = t2.parentid and t1.rating < t2.rating 
join mytable parents 
on parents.id = t1.parentid 
where t2.rating is null and parents.name like '%mike%' 

这里是一个working example

地址:

ORDER BY child.rating DESC 
+0

我做了它仍然是返回戴夫和欧文 – user892134 2012-02-26 12:25:45

+0

你为什么按位分组?它应该没有这个工作,或尝试按评级分组 – Mukesh 2012-02-26 12:43:52

这是作品的MySQL的方式组,实际上是正常工作。

有它周围两路,要么一个子查询或加入该得到的最顶端的孩子,你可能要重新排序表是

的方式这里的连接方法(如果我理解您的数据正确):

SELECT child.* 
FROM mytable parent 
LEFT JOIN mytable child ON parent.parentid=child.parentid 
LEFT JOIN mytable child2 ON child.parentid=child2.parentid AND child2.rating > child.rating AND child2.parentid IS NULL 
WHERE parent.name LIKE '%mike%' AND parent.position = 1 AND child.position <> 1 

这使得假设,parants总是有位置1,而孩子没有。您可能还需要在child2加入另外一点以消除家长比孩子有更高评分的可能性?

第二次加入确保没有其他孩子对每位家长的评分较高。

您可以使用子查询from子句先弄清楚什么是最大额定值为每个父,然后与评价得到孩子:

select * 
from mytable c 
join 
    (select parentid, max(rating) as 'maxrating' 
    from mytable m 
    group by parentid) as q on c.parentid=q.parentid and c.rating = q.maxrating; 
+0

在从中的子查询是需要,确切地说 – Tahbaza 2012-02-26 13:02:28

+0

实际上,它不是'需要' – 2012-02-26 13:03:42

这一定是你想要做什么(虽然我不确定if're确实比较孩子的parentid与父母的parentid):

SELECT child.* FROM mytable child 
    INNER JOIN mytable parent on parent.parentid=child.parentid 
    LEFT JOIN mytable child2 ON (child2.parentid = parent.parentid AND child2.position = child.position AND child2.rating > child.rating) 
    WHERE parent.name LIKE '%mike%' AND child2.parentid IS NULL 
    GROUP BY child.position, child.parentid 
    HAVING `better` = 0; 

另一种选择是使用子查询,但你应该检查哪些工作速度快:

SELECT child.* 
FROM (
    SELECT MAX(child.rating) maxrating, child.parentid, child.position FROM mytable child 
     INNER JOIN mytable parent on parent.parentid=child.parentid 
     WHERE parent.name LIKE '%mike%' 
     GROUP BY child.position,child.parentid 
) h 
    INNER JOIN mytable child ON (child.parentid = h.parentid AND child.position = h.position AND child.rating = h.maxrating) 

不同大小的表格可能会有很大差异。

如果我没有明白你的观点,我仍然建议你使用INNER JOIN而不是OUTER,如果你不需要任何东西来加入。 INNER JOINs通常会更快。

我其实认为第二个会在更大的桌子上工作得更快。