gpt4 book ai didi

mysql - 如何让这个查询运行得更快?

转载 作者:行者123 更新时间:2023-11-29 09:02:23 24 4
gpt4 key购买 nike

我有这样的查询:

    SELECT 
`om_chapter`.`manganame` as `link`,
(SELECT `manganame` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `manganame`,
(SELECT `cover` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `cover`,
(SELECT `othername` FROM `om_manga` WHERE `Active` = '1' AND `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AS `othername`
FROM `om_chapter`
WHERE
`Active` = '1' AND
(SELECT `Active` From `om_manga` WHERE `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AND
`id` IN ( SELECT MAX(`id`) FROM `om_chapter` WHERE `Active` = '1' GROUP BY `manganame` )
ORDER BY `id` DESC LIMIT 10

那么我怎样才能使这个查询更快呢?

这是我的表格:

om_chapter:

id  | manganame     | chapter   | Active
-----------------------------------------
1 | naruto | 1 | 1
2 | naruto | 12 | 1
3 | naruto | 22 | 1
4 | bleach | 10 | 1
5 | bleach | 15 | 1
6 | gents | 1 | 1
7 | naruto | 21 | 1

om_manga:

id  | othername | manganame     | cover     | Active
-----------------------------------------------------
1 | naruto | naruto | n.jpg | 1
2 | bleach | bleach | b.jpg | 1
4 | gents | gents | g.jpg | 1

我想要从这个查询中得到的第一件事是通过对 manganame 进行分组并按 id 排序来给我 om_chapter 的最后 10 行..我尝试通过使用组甚至不同来使用一个简单的查询,但它们都没有给我正确的结果...

在使用group或distinct的简单查询中,结果如下:

id  | manganame     | chapter   | Active
-----------------------------------------
7 | prince | 21 | 1
5 | gent | 15 | 1
2 | naruto | 12 | 1
1 | bleach | 1 | 1

但我想要这个结果:

id  | manganame     | chapter   | Active
-----------------------------------------
9 | gents | 21 | 1
8 | bleach | 21 | 1
7 | prince | 21 | 1
6 | naruto | 1 | 1

所以我用这个:

WHERE 
`Active` = '1' AND
(SELECT `Active` From `om_manga` WHERE `om_manga`.`link` = `om_chapter`.`manganame` LIMIT 0,1) AND
`id` IN ( SELECT MAX(`id`) FROM `om_chapter` WHERE `Active` = '1' GROUP BY `manganame` )

我在其中使用子选择,因为我希望 om_manga 表中的 Active 字段为 1..

对于子选择的重置,我实际上没有尝试加入,但我会..!

最佳答案

我可能误解了你的意图..但这里有一个尝试:

SELECT c.`manganame` AS `link`
, m.`manganame`
, m.`cover`
, m.`othername`
FROM
`om_manga` m
INNER JOIN `om_chapter` c
ON m.`link` = c.`manganame`
INNER JOIN
( SELECT `manganame`, MAX(`id`) AS `maxid`
FROM `om_chapter`
WHERE `Active` = '1'
GROUP BY `manganame` ) mx
ON mx.`maxid` = c.`id`
ORDER BY c.`id` DESC LIMIT 10

关于mysql - 如何让这个查询运行得更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8232245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com