Feature. QueryBuilder. Query union. - #6015
Conversation
|
I don't think replacing assertEquals() with assertSame() is the correct behavior for checking code style. |
|
With this PR, I understand we can't write a query to use an (SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10; |
|
Yes, such code is not possible by default. But it can be wrapped in another query using $union = $this->db->table('user')->limit(1)->orderBy('id', 'ASC');
$builder = $this->db->table('user')->union($union)->limit(1)->orderBy('id', 'DESC');
$result = $this->db->newQuery()->fromSubquery($builder, 'q')->orderBy('id', 'DESC')->get();SELECT * FROM (
SELECT * FROM (SELECT * FROM user ORDER BY id DESC LIMIT 1)
UNION
SELECT * FROM (SELECT * FROM user ORDER BY id ASC LIMIT 1)
) q ORDER BY id |
3610fc9 to
c257634
Compare
|
I would like the test code to follow the Arrange Act Assert pattern as much as possible. There are many test codes that do not follow it, though. |
| - QueryBuilder raw SQL string support | ||
| - Added the class ``CodeIgniter\Database\RawSql`` which expresses raw SQL strings. | ||
| - :ref:`select() <query-builder-select-rawsql>`, :ref:`where() <query-builder-where-rawsql>`, :ref:`like() <query-builder-like-rawsql>`, :ref:`join() <query-builder-join-rawsql>` accept the ``CodeIgniter\Database\RawSql`` instance. | ||
| - QueryBuilder. Union queries. |
There was a problem hiding this comment.
If there is a link to the detailed page, readers can go and see it easily.
|
@iRedds I think the query example to use an ORDER BY or LIMIT clause to sort or limit the entire UNION result should be documented. |
c257634 to
a3ccae4
Compare
|
@kenjis I've added an example to the documentation, but now @lonnieezell has nothing to add to the new version of the book ))) |
|
@iRedds Do you say about https://leanpub.com/codeigniter4foundations ? |
|
@kenjis yes, i do |
michalsn
left a comment
There was a problem hiding this comment.
Excellent addition.
There are small additions to the docs I would like to see, though.
Clarification for union() method. Co-authored-by: Michal Sniatala <michal@sniatala.pl>
Clarification for unionAll() method. Co-authored-by: Michal Sniatala <michal@sniatala.pl>
|
Thanks to everyone who helped along this long journey. |
Description
This is the second attempt to add a query union to QueryBuilder.
Ref #4291
The implementation provides two methods:
BaseBuilder::union(BaseBuilder|Closure $union)BaseBuilder::unionAll(BaseBuilder|Closure $union)The union() method can be called in any order relative to the main query, but the union query will always be appended to the end. That is, the
LIMITorORDER BYclauses will be relative to the main query.Since DBMSs (MSSQL and Oracle) are demanding on queries using
LIMITandORDER BY, when generating SQL, all queries are wrapped in aSELECT * FROM(....) aliasquery.The alias uwrp0 is used for the main query. Each subsequent query will have an alias with index +1.
Checklist: