Simplified, for example, I have two tables, groupsand items.
items (
id,
groupId,
title
)
groups (
id,
groupTitle,
externalURL
)
The regular query that I make looks something like this:
SELECT
i.`id`,
i.`title`,
g.`id` as 'groupId',
g.`groupTitle`,
g.`externalURL`
FROM
items i INNER JOIN groups g ON (i.`groupId` = g.`id`)
However, I need to change this now because all the groups that indicate externalURLwill not have the corresponding records in the table items(since they are stored externally). Is it possible to make some kind of connection so that the result looks like this:
items:
id title groupId
----------------------
1 Item 1 1
2 Item 2 1
groups
id groupTitle externalURL
-------------------------------
1 Group 1 NULL
2 Group 2 something
3 Group 3 NULL
Query output:
id title groupId groupTitle externalURL
---------------------------------------------------
1 Item 1 1 Group 1 NULL
2 Item 2 1 Group 1 NULL
NULL NULL 2 Group 2 something
-- note that group 3 didn't show up because it had no items OR externalURL
Is this possible in a single SQL query?
source
share