What's wrong with this inner query (MySQL)...

Posted by stuboo on Stack Overflow See other posts from Stack Overflow or by stuboo
Published on 2010-04-01T12:43:19Z Indexed on 2010/04/01 13:03 UTC
Read the original article Hit count: 133

Filed under:
|

...besides the fact that I am a total amateur?

My table is set up like this:

CREATE TABLE `messages` (
 `id` int(6) unsigned NOT NULL AUTO_INCREMENT,
 `patient_id` int(6) unsigned NOT NULL,
 `message` varchar(255) NOT NULL,
 `savedate` int(10) unsigned NOT NULL,
 `senddate` int(10) unsigned NOT NULL,
 `SmsSid` varchar(40) NOT NULL COMMENT 'where we store the cookies
from twilio',
 `sendorder` tinyint(3) unsigned NOT NULL COMMENT 'the order we want
the msg sent in',
 `sent` tinyint(1) NOT NULL COMMENT '0=queued, 1=sent,
2=sent-unqueued,4=rec-unread,5=recd-read',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=143 ;

I need a query that will

SELECT * FROM `messages` WHERE `senddate` < $now AND `sent` = 0 (AND LIMIT
TO ONLY ONE RECORD PER `patient_id`)

I've tried the following:

SELECT * 
FROM `messages`
WHERE `senddate` IN 
    (SELECT `patient_id`, max(`senddate`)
     GROUP by `patient_id`) 
AND `senddate` < $now AND `sent` = 0 ;

But I get this error: MySQL client version: 5.1.37

`#1064 - 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 'GROUP by patient_id) AND senddate < 1270093898 AND sent = 0 LIMIT 0, 30' at line 5

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql