来自另一个表的LEFT JOIN计数

时间:2015-09-17 12:12:50

标签: mysql sql

我正在尝试计算一个课程完成了多少交易,我试图加入training_transactions,其中包含training_transaction_course = course_id和training_transaction_status ='已完成的所有行的计数。这是我到目前为止的代码:

SELECT    training.*, 
          Count(DISTINCT training_transactions.training_transaction_course) AS completed_training_payments
left JOIN users 
ON        training.course_user = users.user_id 
LEFT JOIN training_transactions 
ON        training.course_user = training_transactions.training_transaction_user 
FROM      training 
WHERE     course_id = ? 
AND       training_transactions.training_transaction_status = 'complete'
AND       course_enabled = 'enabled'

我的桌子:

培训交易

CREATE TABLE IF NOT EXISTS `training_transactions` (
  `training_transaction_id` int(11) NOT NULL,
  `training_transaction_user` int(11) NOT NULL,
  `training_transaction_course` int(11) NOT NULL,
  `training_transaction_status` varchar(50) NOT NULL,
  `training_transaction_enabled` varchar(50) NOT NULL DEFAULT 'enabled',
  `training_transaction_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

训练

CREATE TABLE IF NOT EXISTS `training` (
  `course_id` int(11) NOT NULL,
  `course_user` int(11) NOT NULL,
  `course_type` varchar(255) NOT NULL,
  `course_name` varchar(255) NOT NULL,
  `course_location` varchar(255) NOT NULL,
  `course_duration` varchar(255) NOT NULL,
  `course_fitness_type` varchar(255) NOT NULL,
  `course_instructor_name` varchar(255) NOT NULL,
  `course_price` int(15) NOT NULL,
  `course_start_date` date NOT NULL,
  `course_max_attendees` int(8) NOT NULL,
  `course_accommodation` varchar(255) NOT NULL,
  `course_accommodation_price` varchar(255) NOT NULL,
  `course_status` varchar(50) NOT NULL,
  `course_enabled` varchar(10) NOT NULL DEFAULT 'enabled'
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;

正如您所看到的,我正在尝试将完成的交易计数作为从课程_max_attendees中扣除的计数,然后我可以检查是否还有任何地方。

2 个答案:

答案 0 :(得分:0)

您想选择培训。所以从training中选择。您希望使用它显示事务计数,您可以在select子句的子查询中执行此操作:

select
  t.*,
  (
    select count(*) 
    from training_transactions tt
    where tt.training_transaction_user = t.course_user
    and tt.training_transaction_status = 'complete'
  ) as completed_training_payments
from training t
where t.course_id = ? 
and t.course_enabled = 'enabled';

以下是联接:

select
  t.*, coalesce(tt.cnt, 0) as completed_training_payments
from training t
left join
(
  select training_transaction_status, count(*) as cnt
  from training_transactions
  where training_transaction_status = 'complete'
  group by training_transaction_status
) tt on tt.training_transaction_user = t.course_user
where t.course_id = ? 
and t.course_enabled = 'enabled';

答案 1 :(得分:0)

首先,如果您想知道每个课程已经完成了多少已完成的交易,您就无法获得所涉及的用户表。您将汇总任何用户信息。

然后,您必须从课程表开始,该表似乎已命名为get_planet_name。现在,您想要计算每个课程的每个已完成的交易。左连接对此非常合适:

training

这个问题是它会给出计数值" 1"对于每个已完成交易的课程,以及那些根本没有完成交易的课程!所以每一行都有" 1"会怀疑的。解决方案是计算密钥,而不是行。这是使用select t.Name, count( * ) as completed_training_payments from training t left join training_transactions tt on tt.user = t.course_user and tt.status = 'complete' where t.course_status = 'enabled' group by t.Name; 而不是sum函数完成的。

count

由于select t.Name, sum( case when tt.course_user is null then 0 else 1 end ) as completed_training_payments from training t left join training_transactions tt on tt.user = t.course_user and tt.status = 'complete' where t.course_status = 'enabled' group by t.Name; 只有tt.course_user,当根本没有完成的交易时,该课程将显示" count" " 0"。