你有没有尝试过
...
AND (a.sequence=0 OR a.sequence=1)
...
我会做连接而不是使用所有那些 WHERE 语句来优化这个查询。
SELECT
a.modelyear,
count(*) as yearcount
FROM
styles a
INNER JOIN jpgs b ON (a.styleid = b.styleid)
INNER JOIN models c ON (c.modelid = a.modelid)
INNER JOIN divisions d ON (d.divisionid = c.divisionid)
WHERE
a.mktclassid in ($this->mktclassids)
AND (a.sequence=0 OR a.sequence=1)
$this->where
GROUP BY
a.modelyear;
上面的查询将获取所有序列 1 或 0 的项目,下一个查询让我思考了一些问题。我将使用查询注释进行解释#
,但它应该获取不具有 1 和 0 序列的项目:
SELECT
a.modelyear,
count(*) as yearcount
FROM
styles a
INNER JOIN jpgs b ON (a.styleid = b.styleid)
INNER JOIN models c ON (c.modelid = a.modelid)
INNER JOIN divisions d ON (d.divisionid = c.divisionid)
WHERE
a.mktclassid in ($this->mktclassids)
#above same as previous
#and where modelid is in modelids either having sequence 0 or 1 but not both:
AND a.modelid IN (
#SELECT modelids that have either sequence 0 or 1 but not both
SELECT
styles.modelid
FROM
styles
WHERE
(
styles.modelid IN (#modelids having sequence 0 but not 1
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=0
GROUP BY
styles.modelid
) AND
styles.modelid NOT IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=1
GROUP BY
styles.modelid
)
)
OR
(
styles.modelid NOT IN (#modelids having sequence 1 but not 0
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=0
GROUP BY
styles.modelid
) AND
styles.modelid IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=1
GROUP BY
styles.modelid
)
)
)
$this->where
GROUP BY
a.modelyear
我希望这不会太混乱
用户声明查询永远运行。试试这个子查询部分,让我知道它的性能。
SELECT
styles.modelid
FROM
styles
WHERE
(
styles.modelid IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=0
GROUP BY
styles.modelid
) AND
styles.modelid NOT IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=1
GROUP BY
styles.modelid
)
)
OR
(
styles.modelid NOT IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=0
GROUP BY
styles.modelid
) AND
styles.modelid IN (
SELECT
styles.modelid
FROM
styles
WHERE
styles.sequence=1
GROUP BY
styles.modelid
)
)
所以我只是想从表中获取不同的模型标识,但最好序列是 0 或 1 - OP
SELECT
modelid,
sequence
FROM
(
SELECT
modelid,
sequence
FROM
styles
ORDER BY
sequence ASC
) AS sub_query
GROUP BY
modelid
这正是我需要的。我如何将它作为 where 子句添加到我的原始查询中?- OP
SELECT
a.modelyear,
count(*) as yearcount
FROM styles a, jpgs b, models c, divisions d
WHERE d.divisionid = c.divisionid
AND a.styleid = b.styleid
AND c.modelid = a.modelid
AND a.mktclassid in ($this->mktclassids)
AND a.modelid IN (
SELECT
modelid
FROM
(
SELECT
modelid,
sequence
FROM
styles
ORDER BY
sequence ASC
) AS sub_query
GROUP BY
modelid
)
$this->where
GROUP BY a.modelyear;
这将导致相同的结果:
SELECT
a.modelyear,
count(*) as yearcount
FROM styles a, jpgs b, models c, divisions d
WHERE d.divisionid = c.divisionid
AND a.styleid = b.styleid
AND c.modelid = a.modelid
AND a.mktclassid in ($this->mktclassids)
AND a.modelid IN (
SELECT
modelid
FROM
styles
GROUP BY
modelid
)
$this->where
GROUP BY a.modelyear;
我不认为这是你真正想要的。您不需要将其作为 WHERE 子句,但您需要完全更好的查询。其他表是必需的吗? 相当于什么?$this->where
在考虑了一个小时后,我认为这可能是您真正想要的。这将样式表限制为只有一个 modelid 并将它们排序到最低序列,然后连接到其他表并应用 mktclassid IN 子句和$this->where
子句:
SELECT
a.modelyear,
count(*) as yearcount
FROM
(
SELECT
*
FROM
(
SELECT
*
FROM
styles
ORDER BY
sequence ASC
) subquery
GROUP BY
modelid
) a
INNER JOIN jpgs b ON (a.styleid = b.styleid)
INNER JOIN models c ON (c.modelid = a.modelid)
INNER JOIN divisions d ON (d.divisionid = c.divisionid)
WHERE
a.mktclassid in ($this->mktclassids) AND
$this->where
GROUP BY a.modelyear;
在此之后,我完成了,除了澄清您的问题并请提供sqlFiddle并提供更多预期输出示例之外,我没有其他建议可提供。有时您面临的问题是因为您尝试做的事情是以错误的方式进行。如果可能,请始终说明您为什么要做某事的原因,而不仅仅是这是我的问题……其他人可能能够简化流程并提供解决实际问题的解决方案。祝你好运!