简体   繁体   English

在查询中限制MySQL结果

[英]Limiting MySQL results within query

I'm looking to see if I can get the results I need with a single query, and my MySQL skills are still in their adolescence over here. 我想看看我是否可以通过单个查询得到我需要的结果,而我的MySQL技能仍然处于青春期。

I have 4 tables: shows , artists , venues and tours . 我有4张桌子: showsartistsvenuestours A simplified version of my main query right now looks like this: 我的主要查询的简化版现在看起来像这样:

SELECT * 
     FROM artists AS a, 
          venues AS v, 
          shows AS s 
LEFT JOIN tours AS t ON s.show_tour_id = t.tour_id
    WHERE s.show_artist_id = a.artist_id
      AND s.show_venue_id = v.venue_id
ORDER BY a.artist_name ASC, s.show_date ASC;

What I want to add is a limit on how many shows are returned per artist . 我要添加的是每个艺术家返回多少节目的限制。 I know I could SELECT * FROM artists , and then run a query with a simple LIMIT clause for each returned row, but I figure there must be a more efficient way. 我知道我可以SELECT * FROM artists ,然后为每个返回的行运行一个带有简单LIMIT子句的查询,但我认为必须有一种更有效的方法。

UPDATE: to put this more simply, I want to select up to 5 shows for each artist. 更新:更简单地说,我想为每位艺术家选择最多5个节目。 I know I could do this (stripping away all irrelevancies): 我知道我可以做到这一点(剥离所有无关紧要):

<?php
    $artists = $db->query("SELECT * FROM artists");
    foreach($artists as $artist) {
        $db->query("SELECT * FROM shows WHERE show_artist_id = $artist->artist_id LIMIT 5");
    }
?>

But it seems wrong to be putting another query within a foreach loop. 但是在foreach循环中放置另一个查询似乎是错误的。 I'm looking for a way to achieve this within one result set. 我正在寻找一种在一个结果集中实现这一目标的方法。

This is the kind of thing stored procedures are for. 这是存储过程的用途。

Select a list of artists, then loop through that list, adding 5 or fewer shows for each artists to a temp table. 选择一个艺术家列表,然后遍历该列表,为每个艺术家添加5个或更少的节目到临时表。

Then, return the temp table. 然后,返回临时表。

As a plan-B, if you can't figure the proper SQL statement to use you can read the whole thing into a memory construct (array, class, etc) and loop it that way. 作为计划-B,如果你无法想象使用正确的SQL语句,你可以将整个事物读入内存构造(数组,类等)并以这种方式循环。 If the data is sufficiently small and memory available sufficiently large this would let you do only one query. 如果数据足够小并且内存可用足够大,那么您只能进行一次查询。 Not elegant, but may work for you. 不优雅,但可能适合你。

Well I hesitate to suggest this because it certainly won't be computationally efficient (see the stored procedures answer for that...) but it will all be in one query like you wanted. 嗯,我犹豫是否建议这一点,因为它肯定不会在计算上有效(请参阅存储过程的答案......)但它将全部在您想要的一个查询中。 I'm also taking some liberties and assuming that you want the 5 most recent shows...hopefully you can modify to your actual requirements. 我也有一些自由,假设你想要最近的5个节目......希望你可以修改你的实际要求。

SELECT * 
 FROM artists AS a, 
      venues AS v, 
      shows AS s 
LEFT JOIN tours AS t ON s.show_tour_id = t.tour_id
WHERE s.show_artist_id = a.artist_id
  AND s.show_venue_id = v.venue_id
  AND s.show_id IN
     (SELECT subS.show_id FROM shows subS 
      WHERE subS.show_artist_id = s.show_artist_id
      ORDER BY subS.show_date DESC
      LIMIT 5)
ORDER BY a.artist_name ASC, s.show_date ASC;

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

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