-1

我得到了正确的回应。

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

如果有人能告诉我我做错了什么,我将不胜感激。

4

1 回答 1

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. 您只需要 1 个,其中:

     WHERE be_user_profiles.advisor = $id
     AND hw_homework.date  >= $from AND  <= $to // neither AND WHERE hw_homework.date BETWEEN $from AND $to
    
  2. 我不确定评论是关于什么的:

    WHERE be_user_profiles.advisor = $id
    AND hw_homework.date  >= $from AND  <= $to 
    
  3. 您应该添加'日期:

    WHERE be_user_profiles.advisor = $id
    AND hw_homework.date  >= '$from' AND  <= '$to'
    
  4. 最后,您需要将该列添加到<=

    WHERE be_user_profiles.advisor = $id
    AND hw_homework.date  >= '$from' AND  hw_homework.date <= '$to'
    

最后一个查询应该有效。

上次编辑后:您似乎运行的实际查询的问题是第 4 点:您没有'日期

于 2013-08-19T11:54:11.890 回答