选择未知列中的mysql子查询

时间:2015-07-03 03:15:31

标签: mariadb

我在select语句中有一个子查询,我需要根据基本查询查找计数。如果我硬编码需要计数的course_id有效(其他那个计数仅适用于所有列出的相同课程)但是当我更改它以使用列出的course_id时它返回1054 - 未知列错误。

错误SQL

SELECT 
 c.course_id, 
 c.course_name,
 (SELECT COUNT(wpt_users_id)
  FROM (SELECT course_activity.wpt_users_id, course_activity.course_id, min(course_activity.course_activity_entry_dt)
  FROM wpt_user_subscriptions
  JOIN course_activity ON course_activity.wpt_users_id = wpt_user_subscriptions.wpt_users_id
  WHERE wpt_user_subscriptions.wpt_subscription_id = 35 
  AND course_activity.course_id = c.course_id
  GROUP BY course_activity.wpt_users_id, course_activity.course_id) sub1) as course_count
FROM `wpt_subscription_course` sc
JOIN course c ON c.course_id = sc.course_id
WHERE sc.`wpt_subscription_id` = 35

返回:MySQL说:#1054 - 未知栏&c; c.course_id'在' where子句'

将运行SQL:

SELECT 
 c.course_id, 
 c.course_name,
 (SELECT COUNT(wpt_users_id)
  FROM (SELECT course_activity.wpt_users_id, course_activity.course_id, min(course_activity.course_activity_entry_dt)
  FROM wpt_user_subscriptions
  JOIN course_activity ON course_activity.wpt_users_id = wpt_user_subscriptions.wpt_users_id
  WHERE wpt_user_subscriptions.wpt_subscription_id = 35 
  AND course_activity.course_id = 9
  GROUP BY course_activity.wpt_users_id, course_activity.course_id) sub1) as course_count
FROM `wpt_subscription_course` sc
JOIN course c ON c.course_id = sc.course_id
WHERE sc.`wpt_subscription_id` = 35

当它正确拼写但不能正常工作时,为什么它不能正常工作的任何想法。

环境规格:

[[Database server]]
Server type: MariaDB
Server version: 5.5.41-MariaDB - MariaDB Server
Protocol version: 10

[[phpMyAdmin]]
Version information: 4.4.8, latest stable version: 4.4.10

[[Web server]]
Apache/2.4.6 (CentOS) PHP/5.4.16
Database client version: libmysql - 5.5.41-MariaDB
PHP extension: mysqli Documentation
PHP version: 5.4.16

感谢。

1 个答案:

答案 0 :(得分:0)

更多的缩进使其更加明显:

SELECT  c.course_id, c.course_name, 
      ( SELECT  COUNT(wpt_users_id)
            FROM  
              ( SELECT  ca.wpt_users_id, ca.course_id,
                        min(ca.course_activity_entry_dt)
                    FROM  wpt_user_subscriptions AS wus
                    JOIN  course_activity AS ca ON ca.wpt_users_id =
                                                  wus.wpt_users_id
                    WHERE  wus.wpt_subscription_id = 35
                      AND  ca.course_id = c.course_id   -- HERE
                    GROUP BY  ca.wpt_users_id, ca.course_id
              ) sub1
      ) as course_count
    FROM  `wpt_subscription_course` sc
    JOIN  course c ON c.course_id = sc.course_id        -- HERE
    WHERE  sc.`wpt_subscription_id` = 35

我认为子查询只能在紧邻的查询中查看表。

也许这有效并得到正确答案?

SELECT  c.course_id, c.course_name, course_count
    FROM  `wpt_subscription_course` sc
    JOIN  course c ON c.course_id = sc.course_id
    JOIN  
      ( SELECT  COUNT(*) course_count, ca.wpt_users_id, ca.course_id,
                min(ca.course_activity_entry_dt)
            FROM  wpt_user_subscriptions AS wus
            JOIN  course_activity AS ca ON ca.wpt_users_id = wus.wpt_users_id
            WHERE  wus.wpt_subscription_id = 35
              AND  ca.course_id = c.course_id
            GROUP BY  ca.wpt_users_id, ca.course_id 
      ) sub1
    WHERE  sc.`wpt_subscription_id` = 35