All pastes #904967 Raw Edit

Someone

public text v1 · immutable
·published 2008-02-15 13:14 UTC
rendered paste body
I need to get one image from all galleries which have smallest `order` (first one image).


Table with galleries:

mysql> SELECT id,`order`,mother,title FROM pages WHERE type=4;
+----+-------+--------+-------------------------------------------+
| id | order | mother | title                                     |
+----+-------+--------+-------------------------------------------+
|  9 |     1 |      2 | a:1:{s:2:"lv";s:14:"Viena galerija";}     |
| 10 |     2 |      2 | a:1:{s:2:"lv";s:13:"Otra galerija";}      |
| 11 |     3 |      2 | a:1:{s:2:"lv";s:18:"Vēl cita galerija";}  |
+----+-------+--------+-------------------------------------------+


Table with images:

mysql> select pid,filename,`order` from gallery_images WHERE pid=9;
+------+-------------------------------------------------+-------+
| pid  | filename                                        | order |
+------+-------------------------------------------------+-------+
|    9 | bANd49iLIX_World Djs Day pack_by-DNS1k (3).jpg  |     5 |
|    9 | dHZnSb3KAK_World Djs Day pack_by-DNS1k (20).jpg |     6 |
|    9 | tT70IXasTr_World Djs Day pack_by-DNS1k (18).jpg |     7 |
|    9 | ZSlmOrGHQp_World Djs Day pack_by-DNS1k (17).jpg |     8 |
|    9 | nAmXQB6Nog_World Djs Day pack_by-DNS1k (10).jpg |     9 |
|    9 | JEH0YMTrDd_World Djs Day pack_by-DNS1k (4).jpg  |     4 |
|    9 | NFLCEMO95n_World Djs Day pack_by-DNS1k (6).jpg  |     3 |
|    9 | rGcneMYCWV_World Djs Day pack_by-DNS1k (7).jpg  |     2 |
|    9 | VFpgFkEm1s_World Djs Day pack_by-DNS1k (8).jpg  |     1 |
+------+-------------------------------------------------+-------+

RESULT:

mysql> SELECT t1.id,filename,t2.`order` FROM pages t1 LEFT JOIN gallery_images t2 on pid=t1.id WHERE mother='2' AND type=4 GROUP By title ORDER By t1.`order`;
+----+------------------------------------------------+-------+
| id | filename                                       | order |
+----+------------------------------------------------+-------+
|  9 | bANd49iLIX_World Djs Day pack_by-DNS1k (3).jpg |     5 |
| 10 | XenlI9F9Dk_World Djs Day pack_by-DNS1k (8).jpg |     1 |
| 11 | boksPJSgEg_World Djs Day pack_by-DNS1k (3).jpg |     2 |
+----+------------------------------------------------+-------+


But i am expecting to get first image (smallest `order`) from each gallery.