简体   繁体   English

加入MySQL的LIMIT结果

[英]LIMIT result on join MySQL

take the case you have 2 table, for example tbCostumers and tbOrders. 假设您有2个表,例如tbCostumers和tbOrders。 I would like to display a summary list with all costumers, related orders and display them with a paginator. 我想显示所有客户的摘要列表,相关订单并使用分页器显示它们。

Doing a join I can extract costumers list and all orders for each costumer, the result is something like: 进行联接后,我可以提取出客户列表以及每个客户的所有订单,结果是:

idCostumer | name | ... | idProduct | productName | price | ...

Where the first n columns are all equal if the costumer has more than 1 order. 如果客户的订单数超过1,则前n列均相等。 So I can have: 所以我可以有:

1 | will | ... | 12 | product1 | 123 | ...
2 | bill | ... | 23 | product2 | 321 | ... 
2 | bill | ... | 24 | product3 | 231 | ...

And so on 等等

I'm trying to use LIMIT to extract only n records and using them with a paginator. 我正在尝试使用LIMIT仅提取n条记录并将其与分页器一起使用。

First question: if a costumer has more than 1 order, with this query I'll see n records, equal in the first column (id, name, ... and other costumer info) but different at the end, where there are products info. 第一个问题:如果客户有一个以上的订单,那么在此查询中,我将看到n条记录,它们在第一列(id,name,...和其他客户信息)相等,但最后却不同,那里有产品信息。 Is this 'correct'? 这个对吗'? Is there another way to extract this informations? 还有另一种提取此信息的方法吗?

Second question: if I do that and I use a LIMIT , I could "cut" the result table between 2 (or more) records that represent the same customer; 第二个问题:如果我这样做并且使用LIMIT ,则可以在代表同一客户的2条(或更多)记录之间“切割”结果表; so, for example in the small table above, if I limit with 2 the third row will be lost, even if it's part of the row above, because is just another order of the same costumer. 因此,例如在上面的小表中,如果我限制为2,则第三行将丢失,即使它是上面行的一部分,也因为同一客户的另一个顺序。

I would like to limit the number of different idCostumer, in order to take exactly n costumers, even if they appear more than 1 times in the result table. 我想限制不同的idCostumer的数量,以便准确地容纳n个客户,即使它们在结果表中出现了1次以上。 Something like n different idCostumer, no matter if they are repeated. 就像n个不同的idCostumer一样,无论是否重复。

Is this possible? 这可能吗?

I hope it's clear, it was not easy to explain what I would like to achieve :) 我希望很清楚,要解释我想要实现的目标并不容易:)

Thank you! 谢谢!

You might want to have something like this: 您可能想要这样的东西:

SELECT * FROM (
  (SELECT * FROM tbCustomers LIMIT 3) AS c
  INNER JOIN tbOrders AS o ON o.customer = c.idcustomer 
);

You can substitute the first asterisk with named columns and only receive your desired columns in the order you prefer (ie: SELECT c.name, o.price FROM...) . 您可以用命名列替换第一个星号,并且仅按您喜欢的顺序接收所需的列(即:SELECT c.name,o.price FROM ...)。

Hope this works for you! 希望这对您有用!

EDIT : changing the value of the LIMIT clause changes the number of the picked customers, of course. 编辑 :当然,更改LIMIT子句的值会更改选择的客户数。

EDIT 2 : As Alvaro Pointed out, you'll probably need an order clause in the tbCustomers query. 编辑2 :正如Alvaro指出的那样,您可能需要在tbCustomers查询中使用order子句。

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

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