austin
austin

Reputation: 15

mysql returning 1 row per id

I am trying to join 3 tables. I would like to each project one time if it has a post with media in that post.

SELECT *
FROM `database`.project join
     post
     on post.project_id = post.id join
     media
     on media.post_id = post.id

What i currently get as result

Example

I want to code to return one instance of

Floaty
HeadPhones
Fasion + Technologie

so that it doesn't return multiple projects

Any help would be much appricated

Upvotes: 0

Views: 49

Answers (2)

André Amaral
André Amaral

Reputation: 135

you can use distinct: select distinct project.id. https://www.w3schools.com/sql/sql_distinct.asp

Or you can use group by: group by project.id, project.name... https://www.w3schools.com/sql/sql_groupby.asp

Both of then needs to put all the fields results on the query

Upvotes: 0

Daniel Marcus
Daniel Marcus

Reputation: 2686

SELECT distinct name
FROM database.project join
     post
     on post.project_id = post.id join
     media
     on media.post_id = post.id

Upvotes: 1

Related Questions