Question: QueryBuilder how to join on id columns and a column with arbitrary value?
See original GitHub issueDescribe the feature as you’d like to see it I have a requirement that a join be done on a foreign key and the value of another column.
example query:
select item_id, item_group, group1_table.name as group1_name, group2_table.name as group2_name, group3_table.name as group3_name
from items_used
left join group1_table on item_group='household' and item_id = group1_table.id
left join group2_table on item_group='garden' and item_id = group2_table.id
left join group3_table on item_group='garage' and item_id = group3_table.id
where items_used.user_id = '30745508-bc6f-49fd-a635-18f5d96f5afe'
group by item_id, item_group, group1_name, group2_name, group3_name
order by count(item_id);
This will produce rows where only the groupx_name columns are filled when the join for that item is fully realised example results
| item_id | item_group | group1_name | group2_name | group3_name |
|---|---|---|---|---|
| 6d7d865f-e4e5-4a23-8804-d5cb41dc0fec | household | Box Cutter | ||
| c1793683-ccfe-4cb8-b41d-f9361c737928 | garden | spade |
In Orator you can do it like this example Its obviously not the same as above query but you get the idea
clause = JoinClause('contacts').on('users.id', '=', 'contacts.user_id').where('contacts.user_id', '>', 5)
db.table('users').join(clause).get()
How do we do this in Masonite Orm? I am tempted to build a raw query but that would become fragile and I would like to know how to do this properly as the join documentation does not have any info for this common usage.
Help and pointers would be appreciated.
Thanks
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Doctrine Query Builder with joins resulting in - Stack Overflow
This works fine when all the joins are done through entity relationships, but when I introduce a join not through a relationship (...
Read more >join saved questions-custom column not found · Issue #13649
Query builder - join saved questions-custom column not found #13649 ... Summarize "Count of rows" and by "Product ID", save as Question1
Read more >SQL JOIN TABLES: Working with Queries in SQL Server
In this article, you will see how to use different types of SQL JOIN tables queries to select data from two or more...
Read more >How to Join on Multiple Columns | LearnSQL.com
Problem : You want to join tables on multiple columns by using a primary compound key in one table and a foreign compound...
Read more >Working with Databases: Query Builder - Yii Framework
The select() method specifies the SELECT fragment of a SQL statement. You can specify columns to be selected in either an array or...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

As in where clauses? Yes. https://github.com/MasoniteFramework/orm/pull/471
@josephmancuso
Nice work! Simple, elegant, and obvious to read.