我试图从另一个表中选择行计数,即使它是空的,所以如果它是空的,它只显示数字0但仍然选择主表的行。 / p>
这是我的sql:
SELECT training.*,
count(distinct training_transactions.training_transaction_course) as completed_training_payments
FROM training
INNER JOIN training_transactions
ON training.course_id = training_transactions.training_transaction_course
WHERE course_main = ?
AND course_enabled = 'enabled'
培训表:
CREATE TABLE IF NOT EXISTS `training` (
`course_id` int(11) NOT NULL,
`course_user` int(11) NOT NULL,
`course_main` int(11) NOT NULL,
`course_type` varchar(255) NOT NULL,
`course_name` varchar(255) NOT NULL,
`course_description` text 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',
`course_location_name` varchar(255) NOT NULL,
`course_location_street` varchar(255) NOT NULL,
`course_location_town` varchar(255) NOT NULL,
`course_location_county` varchar(255) NOT NULL,
`course_location_postcode` varchar(255) NOT NULL,
`course_location_country` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `training`
--
INSERT INTO `training` (`course_id`, `course_user`, `course_main`, `course_type`, `course_name`, `course_description`, `course_location`, `course_duration`, `course_fitness_type`, `course_instructor_name`, `course_price`, `course_start_date`, `course_max_attendees`, `course_accommodation`, `course_accommodation_price`, `course_status`, `course_enabled`, `course_location_name`, `course_location_street`, `course_location_town`, `course_location_county`, `course_location_postcode`, `course_location_country`) VALUES
(1, 3, 4, 'Health & Safety', 'lol', 'This is just a short description, this can be editted', '1', '13', 'lol', 'lol', 5, '1991-02-12', 4, '1', '4', 'live', 'enabled', '', '', '', '', '', 'United Kingdom'),
(2, 3, 4, 'Working at Height', 'lol', '', '1', '11', 'jkjkj', 'kjkjkj', 124, '0000-00-00', 6, '0', '', 'live', 'enabled', '', '123', '123', '123', 'WN8', 'United Kingdom'),
(3, 3, 4, 'Working at Height', 'lol', '', '1', '11', 'jkjkj', 'kjkjkj', 124, '0000-00-00', 6, '0', '', 'live', 'enabled', '', '123', '123', '123', 'WN8', 'United Kingdom');
training_transactions
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 DEFAULT 'pending',
`training_transaction_payment_status` varchar(50) NOT NULL,
`training_transaction_cost` int(11) NOT NULL,
`training_transaction_enabled` varchar(50) NOT NULL DEFAULT 'enabled',
`training_transaction_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`training_transaction_billing_name` varchar(250) NOT NULL,
`training_transaction_billing_address1` varchar(250) NOT NULL,
`training_transaction_billing_address2` varchar(250) NOT NULL,
`training_transaction_billing_city` varchar(250) NOT NULL,
`training_transaction_billing_state` varchar(250) NOT NULL,
`training_transaction_billing_postcode` varchar(250) NOT NULL,
`training_transaction_billing_country` varchar(250) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `training_transactions`
--
INSERT INTO `training_transactions` (`training_transaction_id`, `training_transaction_user`, `training_transaction_course`, `training_transaction_status`, `training_transaction_payment_status`, `training_transaction_cost`, `training_transaction_enabled`, `training_transaction_date`, `training_transaction_billing_name`, `training_transaction_billing_address1`, `training_transaction_billing_address2`, `training_transaction_billing_city`, `training_transaction_billing_state`, `training_transaction_billing_postcode`, `training_transaction_billing_country`) VALUES
(1, 3, 1, 'pending', 'complete', 0, 'enabled', '2015-09-17 14:02:29', *removed my address*);
目前只显示ID为1的培训课程,因为交易中存在一行,如果在training_transactions中没有id匹配,我怎样才能显示所有培训?
答案 0 :(得分:0)
INNER JOIN仅选择两个表中包含连接键的记录的记录。
使用LEFT JOIN / RIGHT JOIN,可以选择一边不存在记录的选项;他们的字段值将为NULL(当计算时,它们应为零,但我并不完全确定)
答案 1 :(得分:0)
使用LEFT JOIN而不是内部联接来列出联接左侧表中的所有记录,另外需要将count()放在子查询中以获取训练表中的所有字段:
C runtime
答案 2 :(得分:0)
有时使用子查询会更容易。试试这个:
SELECT training.*,
(SELECT COUNT(distinct training_transactions.training_transaction_course)
FROM training_transactions
WHERE training.course_id = training_transactions.training_transaction_course) AS completed_training_payments
FROM training
WHERE training.course_main = ?
AND training.course_enabled = 'enabled'