You can use a SELECT ... INTO var_list
query to COUNT
how many entries are already in the database that match your times:
I'm assuming that you mean one entry per day, and I'm assuming that the performance_date
column is of DATETIME
or TIMESTAMP
type.
DECLARE k INT DEFAULT 0;
/* Count number of performances occurring on the same date as the
performance being inserted */
SELECT COUNT(*)
FROM tbl
WHERE performance_date
BETWEEN DATE(NEW.`performance_date`)
AND DATE(DATE_ADD(NEW.`performance_date`, INTERVAL 1 DAY))
INTO k;
/* If k is not 0, error as there is already a performance */
IF k != 0 THEN
SIGNAL SQLSTATE '58005'
SET MESSAGE_TEXT = 'Wrong! Only 1 performance in 1 club is allowed per day! Change your date, or club!';
END IF;
For clarity, if you have a performance with performance_date
as 2014-01-01 19:00:00, and you insert a new performance with date 2014-01-01 08:30:00 (for example) then the above code will run this query, which will return a COUNT of 1, which will then cause the trigger to give that error:
SELECT COUNT(*)
FROM tbl
WHERE performance_date
BETWEEN DATE("2014-01-01 08:30:00") AND DATE(DATE_ADD("2014-01-01 08:30:00", INTERVAL 1 DAY))
# The line above will become:
# BETWEEN "2014-01-01" AND "2014-01-02"
INTO k