-2

我有一个 mysql 表,其中包含一个名为序列的列。

这是我的声明:

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.sequence = 0
$this->where
GROUP BY a.modelyear;

上面的查询应该输出等于这个查询的计数:

select count(distinct(modelid)) from styles where mktclassid in ($this->mktclassids)
$this->where

我的问题是,为了获得计数的每个唯一模型。序列可以等于 0 或 1,但不能同时等于两者。所以一个modelid可能有一个序列= 0。如果它不等于0,那么它将等于1。我需要在where子句中使用if语句。

这就是我认为我需要的:

if modelid has a.sequence = 0, then use a.sequence = 0
else modelid does not have a.sequence = 0, then use a.sequence = 1

我不是 mysql 人,如果我没有表达我需要的东西,我很抱歉。基本上如果模型 ID 没有序列 = 0,那么它将有序列 = 1

样式表对每个模型都有许多修剪级别。第一个修剪将等于 0 或 1。这是样式表的示例

styleid modelid sequence
350422  25156   1
350425  25156   4
350426  25156   5
350427  25156   7
350428  25156   11
350429  25156   6
350432  25156   9
350433  25156   10
356717  25156   8

styleid modelid sequence
308367  18875   0
308368  18875   1
308369  18875   2
308370  18875   3
308371  18875   4

我试图从这个表中得到一个不同的模型标识。我不相信我可以使用 DISTINCT,因为它会影响我的整个查询。

所以你可以看到这个模型没有序列 = 0,所以我需要使用序列 = 1,也许 a 包含 1 或 0...我不确定是否有任何帮助,非常感谢

4

1 回答 1

1

你有没有尝试过

...
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并提供更多预期输出示例之外,我没有其他建议可提供。有时您面临的问题是因为您尝试做的事情是以错误的方式进行。如果可能,请始终说明您为什么要做某事的原因,而不仅仅是这是我的问题……其他人可能能够简化流程并提供解决实际问题的解决方案。祝你好运!

于 2013-07-19T16:36:57.580 回答