我在MySQL查詢中遇到了一些問題。使用組和多個表的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;
購買你有多個圖像的每個時刻?你無法獲得預期的輸出。這三個文件名都不是附在166:F50MTTZK,VPGPFMMS,8J1OT7IT上。 –
結果是正確的,每個時刻有5個圖像。如果你只想選擇一張圖片,mysql應該在哪些條件下選擇一張圖片?最新的?最老的?隨機? – Kaii
@Kaii,我想從rowset中得到第一張圖片,如果有可能按名稱或日期排列,那麼這將是完美的 – sanneo