我有这样的查询:
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` )
我使用sub select in where因为我希望om_manga表中的Active字段为1 ..
对于子选择的重置,我实际上没有尝试加入,但我会......!
答案 0 :(得分: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
答案 1 :(得分:1)
我会在om_chapter表中引入一个外键contstrain,以解释从漫画到相应章节的链接。
这就是我将问题概念化的方法。
A manga can have many chapters. A chapter belongs to one manga.
然后我会改变om_chapter表,包含链接到漫画的章节的外键。
ALTER TABLE om_Chapter (
ADD mangaID int references om_Manga (id)
)
然后删除manganame列,因为它现在是多余的
ALTER TABLE om_Chapter (
DROP COLUMN manganame
)
然后你的表格看起来像这样。
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
om_chapter:
id | chapter | Active | mangaID
-----------------------------------------
1 | 1 | 1 | 1
2 | 12 | 1 | 1
3 | 22 | 1 | 1
4 | 10 | 1 | 2
5 | 15 | 1 | 2
6 | 1 | 1 | 4
最后你可以像这样查询表格
SELECT TOP 10 m.Manganame as link,
m.Manganame,
m.cover,
m.othername,
FROM om_manga as m INNER JOIN
om_chapter as c ON m.ID = c.mangaID
WHERE m.active = 1 AND c.active = 1
ORDER BY m.ID DESC
答案 2 :(得分:0)
为什么不进行简单的加入?
SELECT om_chapter.manganame, cover, othername
FROM om_chapter
JOIN om_manga ON om_chapter.manganame = om_manga=manganame
WHERE om_chapter.Active = 1 AND om_manga.Active = 1
除非我误读你的版本。
答案 3 :(得分:0)
使用左外连接(并丢失子查询和后引号):
SELECT c.manganame AS link,
m.manganame AS manganame,
m.cover AS cover,
m.othername AS `othername
FROM om_chapter AS c
LEFT JOIN om_manga AS m
ON c.manganame = m.manganame
WHERE c.Active = '1'
AND c.id IN (SELECT MAX(o.id)
FROM om_chapter AS o
WHERE o.active = 1
GROUP BY o.manganame)
ORDER BY c.id DESC LIMIT 10
如果是我的查询,我可能也会选择“c.id AS id
”。