PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] Why does a UNION ALL query treat the outer ORDER column as unknown?

 March 13, 2022     cakephp, cakephp-3.0, query-builder, sql, union-all     No comments   

Issue

I'm using unionAll() and return the data perfectly, but I need ordernate the data and always return error because the column not exists.

$events = $this->Events
    ->find('available')
    ->where([
        'Events.group_of_event_id IS NULL'
    ])
    ->select('Events.id')
    ->select('Events.name')
    ->select('Events.slug')
    ->select('Events.date_event_start')
    ->select([
        'is_group' => 0
    ]);

$groups = $this->GroupOfEvents
    ->find('available')
    ->select('GroupOfEvents.id')
    ->select('GroupOfEvents.name')
    ->select('GroupOfEvents.slug')
    ->select('GroupOfEvents.date_event_start')
    ->select([
        'is_group' => 1
    ]);

$limit = 10;
$page = 1;

if($this->request->query('limit'))
    $limit = $this->request->query('limit');

if($this->request->query('page'))
    $page = $this->request->query('page');

$offset = ($page - 1) * $limit;

$connection = ConnectionManager::get('default');
$union = $events->unionAll($groups)->epilog(
    $connection
        ->newQuery()
        ->order(['date_event_start' => 'ASC'])
        ->limit($limit)
        ->offset($offset)
);

Return this error:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date_event_start' in 'order clause'


Solution

As the error states, there is no date_event_start column.

Unlinke normale SQL queries, where a non-table prefixed column would fall back to referring to one of the involved tables, similar doesn't happen for union results, with union results you have to explicity refer to the columns as they have been selected.

So you have to make sure that either the columns are selected without a table prefix, or to select and use proper aliases in the ORDER clause. In order to avoid ambiguity, I'd strongly suggest going for the latter, something like

->select(['date_event_start_alias' => 'Events.date_event_start'])

// ...

->select(['date_event_start_alias' => 'GroupOfEvents.date_event_start'])

// ...

->order(['date_event_start_alias' => 'ASC'])

It should be noted that at least with MySQL and Postgres (I'm not sure about other DBMS like SQLite or SQL Server), you actually have to set the alias only for the first SELECT. Setting it for all selects won't do any harm, so I'm including it in the example, but it's not actually necessary.

See also

  • Cookbook > Database Access & ORM > Query Builder > Selecting Data


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing