Aana Saeed
Aana Saeed

Reputation: 299

Showing all rows data related to group by match while using group by

I want to select all the photoes avoiding gallery name repetations.But using group by,I retrieve all the gallery names but I get only one photo related to every gallery.becoz group by only display only one row.

How can I display all the photoes with out repitation of gallery name?ANy alternative approach?.Plz help.

$sql=mysqli_query($db3->connection,"SELECT  *   from photo  group by   gallery_name ");
    while($row=mysqli_fetch_array($sql)){
        $pic_name=$row['p_name'];

        $gallery_name=$row['gallery_name'];
        echo "$gallery_name>$pic_name,";
    }

ANd second alternative approach can be like here but I am still getting gallery name repitation

 $sql=mysqli_query($db3->connection,"SELECT gallery_name   from photo  group by   gallery_name ");
        while($row1=mysqli_fetch_array($sql1)){
            $gallery_name=$row1['gallery_name'];

        $sql=mysqli_query($db3->connection,"SELECT  *   from photo where gallery_name='$gallery_name' ");
        while($row=mysqli_fetch_array($sql)){
            $pic_name=$row['p_name'];

            $gallery_name1=$row['gallery_name'];
            echo "$gallery_name>$pic_name,";
        }}

Upvotes: 1

Views: 136

Answers (4)

Amit Yaron
Amit Yaron

Reputation: 654

Use 'ORDER BY' instead of "GROUP BY".

SELECT *
 ...
GROUP BY ...

is wrong. Groups are used for counting, finding minimum and maximums, and other group action. The rest should depend on your PHP code.

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

If you are looking for all pictures for a specific gallery, use a WHERE clause without group by like this:

  SELECT  *   
  from photo  
  where gallery_name  = '$gallery_name';

Upvotes: 0

sgeddes
sgeddes

Reputation: 62841

I think you might be looking to use the DISTINCT and ORDER BY keywords:

SELECT DISTINCT p_name, gallery_name
FROM Photo
ORDER BY gallery_name, p_name

Good luck.

Upvotes: 0

Sashi Kant
Sashi Kant

Reputation: 13465

Try this::

SELECT  gallery_name, p_name   from photo group by gallery_name, p_name

Upvotes: 0

Related Questions