patternsqlMinor
Is there any particular reason LIMIT 0 is allowed?
Viewed 0 times
allowedlimitanyreasonparticularthere
Problem
I've just tried running
I could see an argument for making it be an error, because it will never return any result, but then again it isn't invalid like
Are there any actual real-world use cases for
SELECT * FROM tbl LIMIT 0, which unsurprisingly, returns 0 results.I could see an argument for making it be an error, because it will never return any result, but then again it isn't invalid like
LIMIT 'HelloWorld'.Are there any actual real-world use cases for
LIMIT 0, or is it just allowed because there's no real reason to not to allow it?Solution
Perhaps a matter of convenience
I once answered a post Table JOIN and CREATE new table extremely slow, mentioning how
There are ways to use
In another post, MySQL Locks while CREATE TABLE AS SELECT, I mentioned under
In terms of end result, using
I agree with your assumption that it was allowed for no particular reason (because there's no real reason to not allow it, as you say), but it sure comes in handy for such occasions.
I once answered a post Table JOIN and CREATE new table extremely slow, mentioning how
CREATE TABLE AS SELECT will create a table and then perform INSERT INTO SELECT to populate the tableThere are ways to use
LIMIT 0 to create a temp table without indexesCREATE TABLE mytable SELECT * FROM othertable LIMIT 0;In another post, MySQL Locks while CREATE TABLE AS SELECT, I mentioned under
OPTION #2 using WHERE 1=2 as a way to create a blank temp table with no indexesCREATE TABLE 1_temp_foo SELECT * FROM crm_companies WHERE 1=2;
ALTER TABLE 1_temp_foo ENGINE=InnoDB;In terms of end result, using
LIMIT 0 and WHERE 1=2 and even WHERE 0 are synonymous.I agree with your assumption that it was allowed for no particular reason (because there's no real reason to not allow it, as you say), but it sure comes in handy for such occasions.
Code Snippets
CREATE TABLE mytable SELECT * FROM othertable LIMIT 0;CREATE TABLE 1_temp_foo SELECT * FROM crm_companies WHERE 1=2;
ALTER TABLE 1_temp_foo ENGINE=InnoDB;Context
StackExchange Database Administrators Q#133173, answer score: 3
Revisions (0)
No revisions yet.