-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature. QueryBuilder. Query union. #6015
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. |
@@ -72,6 +72,7 @@ Database | |||
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a link to the detailed page, readers can go and see it easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
@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. |
Signed-off-by: Andrey Pyzhikov <[email protected]>
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 ))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
@iRedds Do you say about https://leanpub.com/codeigniter4foundations ? |
@kenjis yes, i do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent addition.
There are small additions to the docs I would like to see, though.
Clarification for union() method. Co-authored-by: Michal Sniatala <[email protected]>
Clarification for unionAll() method. Co-authored-by: Michal Sniatala <[email protected]>
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
LIMIT
orORDER BY
clauses will be relative to the main query.Since DBMSs (MSSQL and Oracle) are demanding on queries using
LIMIT
andORDER BY
, when generating SQL, all queries are wrapped in aSELECT * FROM(....) alias
query.The alias uwrp0 is used for the main query. Each subsequent query will have an alias with index +1.
Checklist: