[英](My)SQL query with MAX and LEFT JOIN
I´m using this query: 我正在使用以下查询:
SELECT `servers`.*, `sponsorships`.`sponsorship_id`, MAX(`sponsorships`.`bid`) as maxbid
FROM `sponsorships`
LEFT JOIN `servers`
ON `sponsorships`.`server_id` = `servers`.`id`
WHERE `sponsorships`.`impressions` <= '1000'
GROUP BY `sponsorships`.`server_id`
ORDER BY maxbid DESC
LIMIT 3;
This gives me the following 这给了我以下
[1] => Array
(
[id] => 23
[user_id] => 1
[cache_time] => 1395557635
[sponsorship_id] => 1
[maxbid] => 20
)
But the row with sponsorship_id => 1
is not the one with the maxbid. 但是
sponsorship_id => 1
的行不是maxbid的行。 What do I have to do to get the sponsorship_id
from the maxbid? 我有什么做的就是在
sponsorship_id
从maxbid?
UPDATE #1 更新#1
I have these two tables: 我有这两个表:
sponsorships 赞助
servers 伺服器
and I want this: 我想要这个:
[1] => Array
(
[id] => 23 (servers.id)
[user_id] => 1
[cache_time] => 1395557635
[sponsorship_id] => 3 (sponsorships.sponsorship_id) [see in the result from before I got sponsorship_id 1 and not 3 or 4 as it should be]
[maxbid] => 20 (MAX(`sponsorships`.`bid`))
)
So the problem with the current I have is that I get the max bid and get only one entry per server from the sponsorship table, which is what I want. 因此,当前存在的问题是,我需要最高出价,并且从赞助商表中每台服务器仅获得一个条目。 But the problem I have is that the
maxbid
is from another row than the sponsorship_id
. 但是我的问题是
maxbid
来自不是sponsorship_id
另一行。 So how can I make sure that I get the sponsorship_id
from the same row as maxbid
? 那么,如何才能确保我得到的
sponsorship_id
从同一行maxbid
?
I'm assuming from your LIMIT 3 you want to find the top 3 servers with the highest bids. 我假设您要从LIMIT 3中找到出价最高的前3个服务器。 It might be better to have the servers table on the left of the join and use a double nested query to find the top bids for each servers and to get the id with the top bid from your sponsorship table.
将服务器表放在联接的左侧,并使用双嵌套查询来查找每个服务器的最高出价,并从您的赞助表中获取具有最高出价的ID,可能会更好。
SELECT `servers`.*, c.`sponsorship_id`, c.`maxbidNQ` as maxbid
FROM `servers`
RIGHT JOIN (select `server_id`,`sponsorship_id`, MAX(`bid`) as maxbidNQ from (select * from `sponsorships` WHERE `impressions` <= '1000' ORDER BY `bid` desc) d GROUP BY `server_id`) c
ON c.`server_id` = `servers`.`id`
ORDER BY maxbid DESC
LIMIT 3;
To get the rows from sponsorships
that have the highest bid
for each server_id
, the normal pattern is to do a JOIN operation to an inline view, something like this: 要从
sponsorships
获取对每个server_id
bid
最高的行,通常的模式是对内联视图执行JOIN操作,如下所示:
SELECT m.server_id
, m.maxbid
, o.*
FROM ( SELECT p.server_id
, MAX(p.bid) AS maxbid
FROM sponsorships p
WHERE p.impressions <= '1000'
GROUP BY p.server_id
) m
JOIN sponsorships o
ON o.server_id = m.server_id
AND o.bid = m.maxbid
AND o.impressions <= '1000'
The inline view (aliased as m
) returns distinct values of server_id
along with the maximum bid
value from all the rows that have that server_id. 内联视图(别名为
m
)从包含该server_id的所有行中返回server_id
不同值以及最高bid
。 (ie max bid for each server_id) (即,每个server_id的最高出价)
With that set, we can look back into the sponsorships table, and fine the rows that match. 有了这个设置,我们可以回顾一下Sponsorships表,并细化匹配的行。 Note that there's a potential to get more than one row back, if there is more than one row that have identical values for
(server_id,bid)
. 请注意,如果有多行具有相同的
(server_id,bid)
值,则有可能获得多于一行。
Those rows can then be joined to the servers table, pull the columns you want, and add any ORDER BY and LIMIT that you want, for example: 然后可以将这些行连接到服务器表,拉出所需的列,并添加所需的任何ORDER BY和LIMIT,例如:
SELECT s.*
, o.sponsorship_id
, m.maxbid
FROM ( SELECT p.server_id
, MAX(p.bid) AS maxbid
FROM sponsorships p
WHERE p.impressions <= '1000'
GROUP BY p.server_id
) m
JOIN sponsorships o
ON o.server_id = m.server_id
AND o.bid = m.maxbid
AND o.impressions <= '1000'
LEFT
JOIN servers s
ON s.id = m.server_id
ORDER BY m.maxbid DESC
LIMIT 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.