I am currently writing a lot of MySQL queries and noticed that I can use shorthand validation, like so:
SELECT id, content, date_added FROM table WHERE date_added
(where date_added is a default NULL timestamp and I don't have to ask specifically if it IS NOT NULL to return all results that have a timestamp)
I just need to know if this is advised against, o开发者_开发百科r if this shorthand functionality became available at a certain MySQL version, as I would like to retain backwards compatibility. I was previously certain that this didn't work and I had to write out the whole conditional, but now I cannot confirm.
I wouldn't exactly call this "shorthand validation". I'm not sure where you're getting that from.
The WHERE
clause is just going to check for truthiness. NULL
is not a TRUE
value, all other non-zero values are TRUE
.
Even though this works for you, I would still strongly advise against it. I favor very explicit database queries. We don't need any added voodoo sorcery here...
Stick with
SELECT id, content, date_added
FROM table
WHERE date_added IS NOT NULL;
I actually understand what you mean by your terms shorthand validation, thanks for pointing this out... I have used "WHERE 1" quite a bit to start off a "WHERE clause variable" in PHP, inspired by phpMyAdmin's queries which has been using that for quite a while, so maybe that answers your question at least somewhat...
This is from 2004: http://forums.devshed.com/mysql-help-4/phpmyadmin-where-1t-112118.html
精彩评论