MongoDB: How to query for records where field is null or not set?

If the sent_at field is not there when its not set then: db.emails.count({sent_at: {$exists: false}}) If it’s there and null, or not there at all: db.emails.count({sent_at: null}) If it’s there and null: db.emails.count({sent_at: { $type: 10 }}) The Query for Null or Missing Fields section of the MongoDB manual describes how to query for null … Read more

What is easier to read in EXISTS subqueries? [closed]

Intuitive is …EXISTS (SELECT * .. because you really don’t care The only keyword of importance is EXISTS The choice of …EXISTS (SELECT 1 .. perpetuates the general myths and superstitions around EXISTS (eg comments on the MySQL docs). ANSI standard says “doesn’t matter” It’s more interesting to understand that EXISTS is a semi-join.

SQL – IF EXISTS UPDATE ELSE INSERT INTO

Create a UNIQUE constraint on your subs_email column, if one does not already exist: ALTER TABLE subs ADD UNIQUE (subs_email) Use INSERT … ON DUPLICATE KEY UPDATE: INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE subs_name = VALUES(subs_name), subs_birthday = VALUES(subs_birthday) You can use the VALUES(col_name) function in the … Read more

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

You can’t return two (or multiple) columns in your subquery to do the comparison in the WHERE A_ID IN (subquery) clause – which column is it supposed to compare A_ID to? Your subquery must only return the one column needed for the comparison to the column on the other side of the IN. So the … Read more

MySQL > Table doesn’t exist. But it does (or it should)

Just in case anyone still cares: I had the same issue after copying a database directory directly using command cp -r /path/to/my/database /var/lib/mysql/new_database If you do this with a database that uses InnoDB tables, you will get this crazy ‘table does not exist’ error mentioned above. The issue is that you need the ib* files … Read more