Nested Joins in query
Nested Joins in query
I have a datatable editor instance, using the PHP backend to do most of the heavy lifting.
The main table is a list of users and their posts.
I have another table that converts those users ids into "externalIds".
I then want to join that table with another table, which contains those users "external posts"
And ideally I want to join those with another table which contains those "external posts tags"
Is it possible to do nested joins like this?
i.e. the query i want to imitate looks like:
SELECT
user_id,
external_id,
COUNT(tags)
FROM users
LEFT JOIN externalUsers
as users.id = externalUsers.internalId
LEFT JOIN externalPosts
as externalPosts.user_id = externalUsers.id
LEFT JOIN externalTags
as externalTags.post_id = externalPosts.id
Is it possible to do something like this?
Editor::inst($db, 'users')
-> fields (
etc...
)
->join(
Mjoin::inst('externalUsers')
->link (etc...)
// and here do another nested join?
Answers
The SQL:
looks a little strange to me. Why are you left joining if all you want to show is fields from the users table? How should this work with an aggregate function but witout GROUP BY? Why would you do an Mjoin in Editor if all you want are LEFT JOINS?
But maybe I am missing something.
And this is what it would look like. Will probably produce poor results though ... Editor can't do GROUP BY by the way, but I resolved it in a different fashion using a getFormatter.
should probably be COUNT(tags) but that would mean that those tags should be in a different table than the users table which they aren't according to your SQL. If each user has just one, why count them? But anyway ...