Is there a simple SELECT-Statement that creates an empty set?

Is there a simple and easy way to create a result table with specified columns but zero rows? In set theory, this is called an empty set, but since relational databases use multidimensional sets, this term does not fit perfectly. I tried these two queries, but both deliver exactly one row, not null rows:

  • SELECT '' AS ID;
  • SELECT null AS ID;

But what I want is the same result as this query:

  • SELECT ID FROM sometable WHERE false;

I am looking for a more elegant way because I do not want to have a linked table, so the query does not depend on any database schema. In addition, a general request may be a little faster (not so that it is important for such a request).

+4
source share
3 answers
SELECT "ID" LIMIT 0; 

Without any real tables.

Note that most (My) SQL clients will simply display "Empty set". However, it really does what you want:

 create table test.test_table select "ID" limit 0; show create table test.test_table\G Table: test_table Create Table: CREATE TABLE `test_table` ( `ID` varchar(2) character set latin1 NOT NULL default '' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin 
+3
source
 SELECT * FROM (SELECT NULL AS ID) AS x WHERE 1 = 0 
+1
source

You can use the DUAL pseudo- DUAL .

 SELECT whatever FROM DUAL WHERE 1 = 0 

Check the documentation (find the DUAL section).

0
source

Source: https://habr.com/ru/post/1400586/


All Articles