mySqli …inner join?

2019-05-30 13:53发布

问题:

I have two tables..photos and galleries with the following structure:
-----------------------------------------------
Table: photos
-----------------------------------------------
--photo ----- owner - gallery - order
123.jpg --------23--------28 ------ 1
234.jpg --------23--------28 ------ 2
345.jpg --------23--------29 ------ 1
456.jpg --------23--------29 ------ 2
567.jpg --------23--------30 ------ 1

------------------------------------------------
Table: galleries
-----------------------------------------------
id --galleryname ---- owner ---- access -
28 -- Gallery 1 ---------- 23 ------- private -
29 -- Gallery 2 ---------- 23 ------- public -
30 -- Gallery 3 ---------- 23 ------- public -

The relation between the two tables is that photos.gallery = galleries.id

I want to get a list (using a while loop) of every gallery with a specific access type (private) and get the photos.photo from the photos table for each gallery where the order = 1.

Should I be using an inner join, left join or what? I tried the below and it did not work. Can someone please help me understand what I am doing wrong?

if ($stmt = $mysqli->prepare("SELECT galleries.id, galleries.galleryname, photos.photo FROM galleries INNER JOIN photos ON galleries.id=photos.gallery WHERE galleries.access = 'private' AND galleries.owner = '23' AND photos.count = '1'")) 
{     
  $stmt->execute();     
  $stmt->bind_result($private_gal_id,$private_gal_name,$private_gal_photo);
  while ($stmt->fetch()) 
    {      
      echo "The results: $private_gal_id - $private_gal_name - $private_gal_photo";
    }
  $stmt->close();;
}

回答1:

Instead of:

AND photos.count = '1'"))

at the end of the query it should be:

AND photos.order = '1'"))



标签: php mysqli