Conditional Joins with QQ::Expand()

Sometimes, you find yourself in a situation when you want to issue a query for ALL items in a given table, and only some information in another table.

For example, let's say you have a list of persons, and a related list of logins. Only some of the persons have logins; some of the logins are disabled. Your task is to show the name of every person, and next to it, show their login information, but only if their login is actually enabled.

Before you found out about conditional joins, you had several options:
  1. Do a LEFT JOIN on the login table; write a database-specific, somewhat convoluted IF statement that might look like IF(login.is_enabled = 1, login.username, ""). But what if you want to show more than just that one column? Write an IF statement for every single output column... Ehh. Plus, not portable across databases.
  2. Get a list of all persons, then also get a list of all logins, then merge the two using PHP. Works with QQuery, but incurs an overhead of extra processing.
As you'd expect, there's a better way. Introducing conditional joins: when you use QQ::Expand, you can specify conditions on the table with which you want to join, and get only those values that you care about. Remember that a QQ::Expand is always a left join - so if a row of a table with which you are joining does not have a matching record, the left side of your join will still be there, and the right side will contain nulls.

Names of every person, plus usernames for each person if their Login is active

kavita 2 arulyfred: none
Kendall2 Publics: none
Ben 5665564: brobinson
Mike Ho: mho
Alex Smith: none
Wendy Smith: none
Karen Wolfe: none
Samantha Jones: none
Linda Brady: none
Jennifer Smithfgfg: none
Brett Carlisle j: none
Jacob Pratt: none
sdf sdf: none
sdhgfhsdg jai: none
billy wilder: none
Nice Job: none
good job: none
moi tpo77: none
test test: none

PROFILING INFORMATION FOR DATABASE CONNECTION #1: 2 queries performed. Please click here to view profiling detail