我得到了正确答案。
SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,
be_user_profiles.last_name
FROM be_user_profiles
LEFT JOIN hw_homework
ON be_user_profiles.user_id= hw_homework.studentid
WHERE be_user_profiles.advisor = $id
GROUP BY be_user_profiles.user_id
ORDER BY COUNT(studentid) DESC
我希望在某些日期之间理清。但如果我添加BETWEEN,则会出错。
SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,
be_user_profiles.last_name
FROM be_user_profiles
LEFT JOIN hw_homework
ON be_user_profiles.user_id= hw_homework.studentid
WHERE be_user_profiles.advisor = $id
AND WHERE hw_homework.date >= $from AND <= $to // neither AND WHERE hw_homework.date BETWEEN $from AND $to
GROUP BY be_user_profiles.user_id
ORDER BY COUNT(studentid) DESC";
我的表结构是这样的。
CREATE TABLE IF NOT EXISTS `hw_homework` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`studentid` int(10) NOT NULL,
`subjectid` int(10) NOT NULL,
`assignment_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`teacherid` int(10) NOT NULL,
`date` datetime NOT NULL,
`date_completed` datetime DEFAULT NULL,
`approved` int(1) unsigned NOT NULL DEFAULT '1',
`entered_by` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=977 ;
--
-- Dumping data for table `hw_homework`
--
INSERT INTO `hw_homework` (`id`, `studentid`, `subjectid`, `assignment_name`, `teacherid`, `date`, `date_completed`, `approved`, `entered_by`) VALUES
(2, 128, 4, 'Asdf', 59, '2012-11-08 00:00:00', NULL, 1, 0),
(3, 96, 12, 'In Class Essay Practise', 67, '2012-11-09 00:00:00', NULL, 1, 0),
(4, 96, 11, 'Creating Want ', 64, '2012-11-09 00:00:00', NULL, 1, 0),
(5, 119, 13, '6A, Q1, 6B, Q1~Q3', 20, '2012-11-12 00:00:00', NULL, 1, 0),
...
...
错误消息
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '00:00:00 AND 2013-06-14
00:00:00 GROUP BY be_user_profiles.user_id ' at line 6
SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,
be_user_profiles.last_name FROM be_user_profiles LEFT JOIN hw_homework ON
be_user_profiles.user_id= hw_homework.studentid WHERE be_user_profiles.advisor = 20
AND hw_homework.date BETWEEN 2012-08-23 00:00:00 AND 2013-06-14 00:00:00 GROUP BY
be_user_profiles.user_id ORDER BY COUNT(studentid) DESC
如果有人能告诉我我做错了怎么办,我感激不尽。
提前致谢。
答案 0 :(得分:1)
您的查询有几个问题,或许您应该查看手册?
WHERE be_user_profiles.advisor = $id
AND WHERE hw_homework.date >= $from AND <= $to // neither AND WHERE hw_homework.date BETWEEN $from AND $to
你只需要1个:
WHERE be_user_profiles.advisor = $id
AND hw_homework.date >= $from AND <= $to // neither AND WHERE hw_homework.date BETWEEN $from AND $to
我不确定评论的内容:
WHERE be_user_profiles.advisor = $id
AND hw_homework.date >= $from AND <= $to
您应该在日期周围添加'
:
WHERE be_user_profiles.advisor = $id
AND hw_homework.date >= '$from' AND <= '$to'
最后,您需要将列添加到<=
WHERE be_user_profiles.advisor = $id
AND hw_homework.date >= '$from' AND hw_homework.date <= '$to'
最后一个查询应该有效。
在您上次修改之后:您似乎运行的实际查询的问题是第4点:您在日期周围没有'