Question

Is there an easy and simple way to create a result table that has specified columns but zero rows? In set theory this is called an empty set, but since relational databases use multidimensional sets the term doesn't fit perfectly. I have tried these two queries, but both deliver exactly one row and not zero 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'm searching for a more elegant way because I don't want to have a table involved, so the query is independent from any database scheme. Also a generic query might be a bit faster (not that it would matter for such a query).

Was it helpful?

Solution

SELECT "ID" LIMIT 0;

Without any real tables.

Do note that most (My)SQL clients simply will display "Empty set". However, it actually 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

OTHER TIPS

SELECT * FROM (SELECT NULL AS ID) AS x WHERE 1 = 0

You can use the DUAL pseudo-table.

SELECT whatever FROM DUAL WHERE 1 = 0

Check the documentation (look for the DUAL section).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top