Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define "literal" tables in SQL?

Is there any SQL subquery syntax that lets you define, literally, a temporary table?

For example, something like

SELECT   MAX(count) AS max,   COUNT(*) AS count FROM   (     (1 AS id, 7 AS count),     (2, 6),     (3, 13),     (4, 12),     (5, 9)   ) AS mytable   INNER JOIN someothertable ON someothertable.id=mytable.id 

This would save having to do two or three queries: creating temporary table, putting data in it, then using it in a join.

I am using MySQL but would be interested in other databases that could do something like that.

like image 577
thomasrutter Avatar asked Jun 12 '09 06:06

thomasrutter


People also ask

What are literals SQL?

The PL/SQL Literals A literal is an explicit numeric, character, string, or Boolean value not represented by an identifier. For example, TRUE, 786, NULL, 'tutorialspoint' are all literals of type Boolean, number, or string. PL/SQL, literals are case-sensitive.

Is literal a data type in SQL?

There are four kinds of literal values supported in SQL. They are : Character string, Bit string, Exact numeric, and Approximate numeric.

What do you mean by literal table?

Table literals are used to pass the name of a table or a placeholder value (instead of a table name) to a query. Table literals appear in the FROM clause of a SQL statement and consist of either the table name, or a SQL variable or API bind variable in place of the table name.

Why we use literals in SQL?

The terms literal and constant value are synonymous and refer to a fixed data value. For example, 'JACK', 'BLUE ISLAND', and '101' are all character literals; 5001 is a numeric literal. Character literals are enclosed in single quotation marks so that Oracle can distinguish them from schema object names.


1 Answers

I suppose you could do a subquery with several SELECTs combined with UNIONs.

SELECT a, b, c, d FROM (     SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS d     UNION ALL      SELECT 5 , 6, 7, 8 ) AS temp; 
like image 66
Blixt Avatar answered Sep 30 '22 08:09

Blixt