简体   繁体   English

根据MySQL和PHP中其他表的列中的最高值选择行

[英]Select rows based on the highest value in a column of an other table in MySQL & PHP

I'm pants at MySQL, so I really need some help with this, please: 我是MySQL的裤子,所以我真的需要一些帮助,请:

I have a table like: 我有一张像这样的桌子:

Pizza              Votes
------------------------
Margherita         1
Pepperoni          7
Sloppy Giuseppe    3

And an other table like: 还有另一个表,例如:

ID    Pizza              Recipe                         Votes
-------------------------------------------------------------
1     Margherita         Jamie Oliver                   1
2     Pepperoni          Hugh Fearnley Whittingstall    7
3     Sloppy Giuseppe    Gino D'Acampo                  3
4     Margherita         Jamie Oliver                   1
5     Pepperoni          Jamie Oliver                   7
6     Sloppy Giuseppe    Jamie Oliver                   3
7     Sloppy Giuseppe    Hugh Fearnley Whittingstall    3

Notice in the first table, the Pepperoni pizza has the most votes, so I'd like to get the rows from the second table WHERE the Pizza column contains "Pepperoni". 请注意,在第一个表中,香肠比萨拥有最多票数,所以我想从第二个表中获取行WHERE比萨列包含“辣”。

In pseudo-code: SELECT * FROM the second table WHERE the Pizza column = the pizza from the first table with the most votes 用伪代码: SELECT * FROM the second table WHERE the Pizza column = the pizza from the first table with the most votes

So far I have this: 'SELECT * FROM pp_pizzas INNER JOIN pp_recipes ON pp_pizzas.pizza = pp_recipes.pizza' 到目前为止,我有: 'SELECT * FROM pp_pizzas INNER JOIN pp_recipes ON pp_pizzas.pizza = pp_recipes.pizza'

I really don't understand all this MySQL stuff so if I could ask for an explanation as well as a pointer/answer I'd be very grateful, just so I can learn for next time! 我真的不理解所有这些MySQL知识,所以如果我能要求我解释一下以及给出一个指针/答案,我将不胜感激,以便下次可以学习!

Thank you all so much! 非常感谢大家!

There are a lot of ways to do this, but for such a simple query (and for clarity) I'd go with a subquery rather than a join. 有很多方法可以做到这一点,但是对于这样一个简单的查询(为了清楚起见),我将使用子查询而不是联接。 Something like: 就像是:

SELECT * FROM `pp_recipes` WHERE pp_recipes.pizza = (SELECT `pizza` FROM pp_pizzas ORDER BY votes DESC LIMIT 1);

The subquery returns only one column from one row (Pepperoni) which is then used as a condition in the primary query. 子查询仅返回一行中的一列(意大利辣香肠),然后将其用作主查询中的条件。

You could try: 您可以尝试:

SELECT * FROM pp_recipes
WHERE Pizza = 
    (SELECT Pizza FROM pp_pizzas
     ORDER BY Votes DESC LIMIT 1)

You could also try: 您也可以尝试:

SELECT * FROM pp_recipes
WHERE Votes = (SELECT MAX(Votes) FROM pp_recipes)

If you are looking to catch all the records from the second table you must use a LEFT JOIN in place of INNER JOIN. 如果要捕获第二个表中的所有记录,则必须使用LEFT JOIN代替INNER JOIN。

Try this one, and let me know if is what you need : 试试这个,让我知道您是否需要:

SELECT * 
FROM 
  pp_pizzas 
LEFT JOIN 
  pp_recipes 
ON pp_pizzas.pizza = pp_recipes.pizza

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

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