Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 294937

Allow select query without a table

$
0
0

Situation

A comment on line 830 of core/lib/Drupal/Core/Database/Query/Select.php says:

// FROM - We presume all queries have a FROM, as any query that doesn't won't need the query builder anyway.

This sounds sensible, but there is in fact a use case for a query without a table. If you want to add an arbitrary result row to a db query, you can do so with a union. The raw query would look something like this (source):

SELECT age, name
FROM users
UNION
SELECT 25 AS age, 'Betty' AS name

As you can see, the second select does not specify a table, and this is valid SQL.

Problem

Currently you can try to build such a query with Drupal's Select::union like this:

$query = $this->select('my_table', 't')
  ->fields('t', [
    'id',
    'title',
  ]);
$extra_row = $this->select('');
$extra_row->addExpression(1000, 'id');
$extra_row->addExpression("'foo'", 'title');
$query->union($extra_row, 'ALL');

However, you would end up with an invalid query because it is assumed that there is always a table:

SELECT age, name
FROM {users}
UNION
SELECT 25 AS age, 'Betty' AS name
FROM {}

Proposed solution

Allow select queries without a table: When no table name is provided, do not print FROM {}.


Viewing all articles
Browse latest Browse all 294937

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>