I'm creating a simple session based app in PHP/MySQL. I'm storing a session for each user connected to app in mysql table with their associated session_id. What I want to do is stop recording once a certain specified number of records are inserted in table. Is it possible in MySQL?
Below is a sample query that I thought would work, but it doesn't.
$dbSessionQ = "INSERT INTO `connSessions` (`sessID`, `expiry`) VALUES ('" . session_id() . "', '3600')";
$dbSessionQ .= "WHERE 开发者_StackOverflow中文版(SELECT COUNT(`sessID`) FROM `connSessions` < 5001) LIMIT 1";
I don't think INSERT has a WHERE clause. To start with, you can first query for the count and then do the insert. (Split the SELECT and the INSERT statements). Once you've figured that out, use a simple transaction to eliminate race conditions.
You could use INSERT...SELECT for this. Something like
INSERT INTO `connSessions` (`sessID`, `expiry`)
SELECT '" . session_id() . "', '3600' FROM `connSessions`
WHERE (SELECT COUNT(`sessID`) FROM `connSessions`) < 5001
But since you are not selecting something from connSessions I do not really see what you want to do here.
精彩评论