0

我对 MySQL 查询有一些问题。

我有 5 个表(括号中的键):

trips (tripId)
trips_moments (tripId, momentId)
moments (momentId)
moments_images (momentIs, imageId)
images (imageId)

在我的情况下,我有 1 次旅行,其中包含 2 个时刻,每个时刻包含 5 个图像。

我希望结果能进入“时刻”:

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

代替:

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

您可以在我的结果中看到 166 和 167 的记录被重复的次数与图像时刻包含的次数一样多。

我目前的查询:

 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

当前表结构:

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;
4

2 回答 2

0

我现在无法测试,但这可以给你一个想法,我改变 moemnt_images join 像这样:

LEFT JOIN `moments_images` AS `mi` ON m.momentId = mi.momentId  
AND imageId = (select min(imageId) from moments_images where momentId=m.momentId)

完整查询:

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  and imageId = (select min(imageId) from moments_images where momentId=m.momentId)
LEFT JOIN `images` AS `i` ON mi.imageId = i.imageId 
WHERE (t.userId = '1') 
GROUP BY `t`.`tripId` 
ORDER BY `t`.`tripId` ASC
于 2012-11-08T23:29:41.910 回答
0

@凯伊

我按两列使用此分组,但结果不如预期:

结果,我在“时刻”字段中获得了两条具有这些值的记录:

第一个记录)

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;6;F50MTTZK;jpg;6R2XJB9X|
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;10;8J1OT7IT;jpg;ZL8WAWCJ|
166;Kościuszko mound in Cracow;2012-11-08 15:38:14;7;9PY1BZD1;jpg;TP7U07ST

第二条记录)

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;22;IS1JPW1N;jpg;31M4XVBM|
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;21;9JOXXPJQ;jpg;ZIVJ5V7K|
167;Pope John Paul II (Jan Paweł 2) home town - Wadowice;2012-11-08 15:57:15;23;OTOWNDZA;jpg;C03UFMBK

当然,如果可能的话,我想得到一个结果,就像我的问题一样。

于 2012-11-08T23:34:03.827 回答