Retrieve unique results from JOIN query

CPuser

I have two tables, one is 'posts' the other is 'images'

posts table

id | article |

 1    |   post 1    |  
 2    |   post 2    |    
 3    |   post 3    |  

images table

project_id | image_name|

 1            |   img1    |  
 1            |   img2    |    
 2            |   img3    | 
 2            |   img4    |  
 3            |   img5    |  
 3            |   img6    |  

My current query is this:

SELECT  * FROM `images`  RIGHT   JOIN `posts` ON images.project_id = posts.id 

But it is showing multiple results from the posts table.

I want the results like this, to avoid multiple results.

post 1 - img1
post 2 - img3
post 3 - img5
DeepThought
SELECT  
  p.article, i.image_name 
FROM 
  `posts` p
JOIN (
  select
    i2.project_id, min(i2.image_name) as image_name
  from
    images i2 
  group by
    i2.project_id
) i
 on i.project_id=p.id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Use result from one query inside a query and join results [Solved]

Different in results from query between query and join

How to write a SQL query that subtracts INNER JOIN results from LEFT JOIN results?

Can't retrieve results from an sqlite query which uses count()

Retrieve results from an EF linq query

In a PostgreSQL query how to filter results from a field in a join

What type of join do I need to use in the below sql query to retrieve the correct results?

Showing results from Mysql Query that don't exist in LEFT JOIN

Issue with query results from an SQL join question. Basic Question

Query to Join Lab Results

One-to-many query with conditional aggregation and how to retrieve distinct results from query

Order and group query results from LEFT JOIN items

MYSQL query SELECT from one table and join results with other table

Exclude records from query based on table join results

Retrieve data from elasticsearch results

MySQL Query Unique Results From Join

MySQL - JOIN or INSERT QUERY RESULTS

Unique results with mysql inner join

SQL - Create a join / query based on the results of another query. The Join table would be from the results of another query

Ruby on Rails get unique country from Query results

MySQL JOIN with LIMIT query results

How to retrieve only 100 results from a Spring JPA query?

MYSQL: Query two tables and join results from second table to an array

mysql query with join repeats results

MySQL - Retrieve results even if join is null

How to query and retrieve results from ~100 customer databases using SpringBoot?

Retrieve query results as dict in SQLAlchemy

Google Sheets Using Results from one query to return results in another (Nest or Join)

Query Results from Two Tables without a JOIN