I’m writing code in which I use SQL to test several different conditions before ultimately deciding what to do. I’m trying to decide between writing a single multipart MySQL query, or writing one query for each condition, with PHP handling the logic of combining them. My questions:
- Are there any good practices or pitfalls that should steer me in either direction?
- If I implement all the logic in a single SQL query, can I get it to return information about how each condition turned out? (see below for explanation.)
Here’s a simplified example. I want to look at how long it’s been since a user has visited the website, and finds an appropriate reminder email to send them. We test 3 independent conditions:
- Which email(s) should be sent based on the time since the user’s last visit?
- For each of those emails, has the user not received this email before?
- Has the user not received any emails from us in the past 3 days?
Here’s an SQL query that checks all 3 of those. (this query is done per user; it uses PDO and variables starting with : are bound parameters):
SELECT message_id, message_content FROM emailtemplates
WHERE time_before_reminder < :days_since_visit
AND message_id not in
(SELECT message_id FROM sentemails WHERE user = :userid)
AND :userid not in
(SELECT userid FROM sentemails WHERE date_add(timesent, interval 3 day) >= now())
ORDER BY time_before_reminder asc
LIMIT 1;
It returns zero rows if any of the conditions fails, or one row if they all pass.
Alternately, here’s some code that does several separate queries. It’s much uglier, but it can give me explanatory output about precisely why we’re not emailing a given user. (I’ve switched to pseudocode so you don’t have to read a zillion $stmt->bindParam($params) lines):
//run the query for condition 1:
query("SELECT message_id, message_content FROM emailtemplates WHERE time_before_reminder < :days_since_visit;")
if 0 rows returned:
echo("no appropriate emails found for this participant")
return false
//then for condition 2:
query("SELECT message_id from sentemails where message_id = :msgid AND user = :userid")
if 1 or more rows returned:
echo("user $userid qualified for message $msgid, but they already received it.");
return false
//then for condition 3:
query("SELECT userid FROM sentemails WHERE user = :userid AND date_add(timesent, interval 3 day) >= now();")
if 1 or more rows returned:
echo("user $userid qualified for message $msgid, but they already received an email from us in the past 3 days. No email sent.")
return false
// if we get here, all tests were passed. Send the message that was found in the first query.
send_reminder($userid, $msgid)
echo("successfully sent message $msgid to user $userid!")
return true
The second example seems really cumbersome, but I like finding out exactly which condition failed and why. Is there a way to have a single query return this kind of information? (I mean returning information that can be parsed into an explanation, of course, not having MySQL write English sentences for me.) Or, is there other advice on handling queries that test several independent conditions like this?
You can probably (tested on MySQL 5.5 without bound parameters) use your conditions in the
SELECTclause, they should come out 0 (failed) or 1 (passed):However, I suppose this means twice the work for the DBMS. It must also be possible to use some
LEFT JOINand see what fields are null… and that’d be more elegant, and probably faster too.Another thing – though irrelevant for your question – what’s the point having an
ORDER BYfollowed by aLIMIT 1?Edit: the
LEFT JOIN-based request should be something like this (tell me if it works, I can’t test it unless I create the DB… and I feel lazy ^^):Expected behavior: zero rows returned means it failed the first test (
time_before_reminder), otherwise ifnullIfNotSentis not null (therefore a validmessage_id) it means it failed the second test (this kind of message has already been sent someday), and ifnullIfMoreThan3daysis not null (a valid user ID) it means the user already received a message in the past 3 days (i.e. failed the third test). So if you get a row withnullIfNotSentandnullIfMoreThan3Days, it means you can send a message using the returnedmessageIdandmessageContent.Please tell if it does work! (or doesn’t) 😉