Note that there are some explanatory texts on larger screens.

plurals
  1. POConcatenation result from MySQL query, using group and multiple tables
    primarykey
    data
    text
    <p>I have some problem with MySQL query.</p> <p>I have 5 tables (keys in brackets):</p> <pre><code>trips (tripId) trips_moments (tripId, momentId) moments (momentId) moments_images (momentIs, imageId) images (imageId) </code></pre> <p>In my situation I have 1 trip, which contains 2 moments, every moment contains 5 images.</p> <p>I'd like in result to get in field "moments":</p> <pre><code>166;Kościuszko mound in Cracow;2012-11-08 15:38:14;6;F50MTTZK;jpg;6R2XJB9X| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;19;66B7VQ84;jpg;ILQYDQUC </code></pre> <p>instead of:</p> <pre><code>166;Kościuszko mound in Cracow;2012-11-08 15:38:14;6;F50MTTZK;jpg;6R2XJB9X| 166;Kościuszko mound in Cracow;2012-11-08 15:38:14;7;9PY1BZD1;jpg;TP7U07ST| 166;Kościuszko mound in Cracow;2012-11-08 15:38:14;8;VPGPFMMS;jpg;VF95BNNQ| 166;Kościuszko mound in Cracow;2012-11-08 15:38:14;9;EC7NL3HC;jpg;PD1CEQE6| 166;Kościuszko mound in Cracow;2012-11-08 15:38:14;10;8J1OT7IT;jpg;ZL8WAWCJ| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;19;66B7VQ84;jpg;ILQYDQUC| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;20;HZIPGJY7;jpg;FOOWJ8BV| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;21;9JOXXPJQ;jpg;ZIVJ5V7K| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;22;IS1JPW1N;jpg;31M4XVBM| 167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;23;OTOWNDZA;jpg;C03UFMBK </code></pre> <p>You can see in my result that record with 166 and 167 is repeated as many times as much images moment contains.</p> <p>My current query:</p> <pre><code> SELECT `t`.`tripId`, `t`.`title`, `t`.`when`, GROUP_CONCAT( DISTINCT( CONCAT( m.momentId, ';', m.title, ';', m.created, ';', i.imageId, ';', i.fileNameBase, ';', i.fileNameExtension, ';', i.directory) ) SEPARATOR '|' ) AS `moments` FROM `trips` AS `t` LEFT JOIN `trips_moments` AS `tm` ON t.tripId = tm.tripId LEFT JOIN `moments` AS `m` ON tm.momentId = m.momentId LEFT JOIN `moments_images` AS `mi` ON m.momentId = mi.momentId LEFT JOIN `images` AS `i` ON mi.imageId = i.imageId WHERE (t.userId = '1') GROUP BY `t`.`tripId` ORDER BY `t`.`tripId` ASC </code></pre> <p>Current table structures:</p> <pre><code>CREATE TABLE IF NOT EXISTS `images` ( `imageId` int(11) NOT NULL AUTO_INCREMENT, `userId` int(11) NOT NULL, `title` varchar(128) NOT NULL, `description` text NOT NULL, `fileNameBase` varchar(64) NOT NULL, `fileNameExtension` varchar(4) NOT NULL, `directory` varchar(64) NOT NULL, `source` varchar(32) NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`imageId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `moments` ( `momentId` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(128) NOT NULL, `userId` int(11) NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`momentId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `moments_images` ( `momentId` int(11) NOT NULL, `imageId` int(11) NOT NULL, PRIMARY KEY (`momentId`,`imageId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `trips` ( `tripId` int(11) NOT NULL AUTO_INCREMENT, `userId` int(11) NOT NULL, `title` varchar(128) NOT NULL, `when` date NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`tripId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `trips_moments` ( `tripId` int(11) NOT NULL, `momentId` int(11) NOT NULL, PRIMARY KEY (`tripId`,`momentId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload