What should be the PHP / MySQL query for the next requirement?

This is a shopping basket where the user can add the item to the watchlist. You have a separate page from the "User" list, where all the apepears products that have been added to the watchlist by this particular user. product_imagesthe table is separate from the product table, since 1 image can have multiple images.

MySQL Tables

Table "product" :
    pid(pk),name,price, etc...

Table "watchlist" :
    wid(pk),pid(fk),userid(fk)

Table "p_images" :
    image_id(pk),pid(fk),image_name

I want to get all the information from the table productand only the 1st image of this particular product from the table p_images. The product must be extracted pid in the table watchlist.

+3
source share
2 answers

I found it accurate using LEFT JOIN to check the following query.

SELECT *
FROM product p
LEFT JOIN P_images i ON p.pid = i.pid
GROUP BY i.pid
0
source
select * from product p
inner join p_images i on p.pid = i.pid
where p.pid = 1
group by p.pid
order by image_id
+3
source

All Articles