I'm having a tough time with a query.
I have a table called project_slugs
that contains the following fields id, project_id, slug, created
.
I have a master table projects
that contains various fields.
There is a foreign key relation via projects.id
and project_slugs.project_id
.
project_slugs
can contain multiple slugs for any given project.
I want to get a single result that has all the fields from the projects
table plus the most recently created projects_slug.slug
for that given project. This should be accomplished by WHERE projects_slug.slug = 'some-slug'
where 'some-slug' may or may not be the most recent slug.
I am able to join the tables successfully, however I am unsure how to incorporate the bolded logic above.
Here is my current query:
SELECT projects.*, project_slugs.slug
FROM `project_slugs` LEFT JOIN
`projects`
ON project_slugs.project_id = projects.id
WHERE project_slugs.slug = 'some-slug'
Slugs:
Projects:
Expected Output with input:
SELECT projects.*, project_slugs.slug
FROM `project_slugs` LEFT JOIN
`projects`
ON project_slugs.project_id = projects.id
WHERE project_slugs.slug = 'star-management-week-2015'
Expected output would be what you see in the last image + the most recent slug
value that is circled as ascertained by project_slugs.created
. Given any project_slugs.slug
(new or old) as the identifier. I'm not even sure if this is possible in sql. The logic is rather simple in php.