This is what outer joins are for.
And you should use proper join syntax (with ON
):
SELECT ...
FROM user_tables LEFT JOIN
(...)
ON user_tables._id = user_table_id
GROUP BY ...
题
In short, I have provided the data, and the current results, please tell me what to do to make the last step. Using SQLite please.
== Written explanation ==
Want to concatenate values from table A using table B, but then display all results from table A even the ones that have no value in table B. I have tried a whole bunch of variations, and I am truly stuck.
Tables:
*user_tables*
_id table_name table_version table_status
---------- ---------- ------------- ------------
1 addresses 1 1
2 jobs 1 1
3 people 1 1
4 phones 1 1
*user_tables_depends*
_id user_table_id user_table_depend_id
---------- ------------- --------------------
1 1 2
2 1 3
3 2 1
4 2 4
5 4 2
Current query:
SELECT table_name, table_version, table_status,
GROUP_CONCAT(dependName, ', ') AS table_dependencies
FROM user_tables JOIN
(SELECT user_table_id, table_name AS dependName
FROM user_tables_depends, user_tables
WHERE user_tables._id = user_table_depend_id)
WHERE user_tables._id = user_table_id
GROUP BY table_name
Query Result:
table_name table_version table_status table_dependencies
---------- ------------- ------------ ------------------
addresses 1 1 jobs, people
jobs 1 1 addresses, phones
phones 1 1 jobs
Desired result:
table_name table_version table_status table_dependencies
---------- ------------- ------------ ------------------
addresses 1 1 jobs, people
jobs 1 1 addresses, phones
people 1 1
phones 1 1 jobs
Note: please don't worry about what the values are, they are dummy data, I just need the query to work as desired in SQLite please. Thanks in advance, cheers.
解决方案
This is what outer joins are for.
And you should use proper join syntax (with ON
):
SELECT ...
FROM user_tables LEFT JOIN
(...)
ON user_tables._id = user_table_id
GROUP BY ...