MySQL在子查询中获取表的行号

时间:2017-07-20 09:14:09

标签: mysql sql

我有一个查询,我需要按特定ID包含表的行数:

SELECT
    (CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
    `op`.`orders_id`,
    (
        SELECT
            COUNT(1) AS `total`
        FROM 
        (
            SELECT
                `op2`.`concert_date`
            FROM
                `orders_products` `op2`
            WHERE
                `op2`.`orders_id` = `op`.`orders_id`
            AND
                `op2`.`concert_date` <> ''
            GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)
        ) AS `e`
    ) AS `devider`
FROM
    `categories` `c`
    JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
    JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
    JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
    `c`.`section_id` = 25
AND
    `p`.`product_type` IN ('P')
AND
    `ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`

主要问题是我收到错误

#1054 - Unknown column 'op.orders_id' in 'where clause'

无法运行此功能。我在循环中分离了查询但是这会导致性能问题,并希望在一个查询中推送它。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

尝试删除子查询并使用COUNT(DISTINCT ..)

SELECT
    (CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
    `op`.`orders_id`,
    (
        SELECT
           COUNT(DISTINCT CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`))
        FROM
          `orders_products` `op2`
        WHERE
          `op2`.`orders_id` = `op`.`orders_id`
        AND
           `op2`.`concert_date` <> ''
    ) AS `devider`
FROM
    `categories` `c`
    JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
    JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
    JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
    `c`.`section_id` = 25
AND
    `p`.`product_type` IN ('P')
AND
    `ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`

你甚至不需要子查询或concat,因为concert_date或concert_time为null

SELECT
    (CAST(`ot`.`value` AS DECIMAL(6,2))) AS `value`,
    `op`.`orders_id`,
    COUNT(DISTINCT `op`.`concert_date`, `op`.`concert_time`) AS `devider`
FROM
    `categories` `c`
    JOIN `products` `p` ON `p`.`section_id` = `c`.`section_id`
    LEFT JOIN `orders_products` `op` ON `op`.`products_id` = `p`.`products_id`
    JOIN `orders_total` `ot` ON `ot`.`orders_id` = `op`.`orders_id`
WHERE
    `c`.`section_id` = 25
AND
    `p`.`product_type` IN ('P')
AND
    `ot`.`class` IN ('ot_shipping')
GROUP BY `op`.`orders_id`

答案 1 :(得分:0)

在这个子查询中:

SELECT
                `op2`.`concert_date`
            FROM
                `orders_products` `op2`
            WHERE
                `op2`.`orders_id` = `op`.`orders_id`
            AND
                `op2`.`concert_date` <> ''
            GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)

您没有选择op作为表格。 应该是:

SELECT
                `op2`.`concert_date`
            FROM
                `orders_products` `op2`, `orders_products` `op`
            WHERE
                `op2`.`orders_id` = `op`.`orders_id`
            AND
                `op2`.`concert_date` <> ''
            GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)

除此之外,您正在进行隐式连接,而您应该使用显式连接,并且子查询应该变为:

SELECT
                `op2`.`concert_date`
            FROM
                `orders_products` `op2` JOIN `orders_products` `op`
            ON
                `op2`.`orders_id` = `op`.`orders_id`
            WHERE
                `op2`.`concert_date` <> ''
            GROUP BY CONCAT(`op2`.`concert_date`,' ',`op2`.`concert_time`)