Forum Moderators: coopster
Items
+------+--------+
¦ItemID¦ColorIDs¦
+------+--------+
¦`````1¦1,2`````¦
+------+--------+
¦`````2¦1```````¦
+------+--------+
¦`````3¦2,3`````¦
+------+--------+
Colors
+-------+---------+
¦ColorID¦ColorName¦
+-------+---------+
¦``````1¦White````¦
+-------+---------+
¦``````2¦Burgundy`¦
+-------+---------+
¦``````3¦SteelBlue¦
+-------+---------+
How can I write a query which will give me a result like this:
+------+------------------+
¦ItemID¦Colors````````````¦
+------+------------------+
¦`````1¦White,Burgundy````¦
+------+------------------+
¦`````2¦White`````````````¦
+------+------------------+
¦`````3¦Burgundy,SteelBlue¦
+------+------------------+
... in other words, where I get the a comma separated list of color names back in place of the comma separated list of color IDs?
So far I have tried:
SELECT IF( LENGTH( @itemid ) = LENGTH( @colors ),
(
@itemid := NULL
AND @colors := NULL
),
'set'
) as Init,
IF( CAST(@itemid AS CHAR) = CAST(i.ItemID AS CHAR),
@colors := CONCAT( @colors, ',', c.ColorName ),
@colors := c.ColorName
) as Setting,
@itemid := CAST(i.ItemID AS CHAR),
i.ItemID,
@colors as Colors
FROM Items i
INNER JOIN Colors c ON FIND_IN_SET(c.ColorID, i.ColorIDs)
WHERE 1
ORDER BY i.ItemID ASC
... but it doesn't seem to work. :(
+------+---------+
¦ItemID¦Colors```¦
+------+---------+
¦`````1¦White````¦
+------+---------+
¦`````1¦Burgundy`¦
+------+---------+
¦`````2¦White````¦
+------+---------+
¦`````3¦Burgundy`¦
+------+---------+
¦`````3¦SteelBlue¦
+------+---------+
When I expect:
+------+------------------+
¦ItemID¦Colors````````````¦
+------+------------------+
¦`````1¦White`````````````¦
+------+------------------+
¦`````1¦White,Burgundy````¦
+------+------------------+
¦`````2¦White`````````````¦
+------+------------------+
¦`````3¦Burgundy``````````¦
+------+------------------+
¦`````3¦Burgundy,SteelBlue¦
+------+------------------+
SELECT ItemID,
ColorList
FROM (
SELECT IF( LENGTH( @itemid ) = LENGTH ( @colors )
AND CONCAT_WS(',', LENGTH( @itemid ), LENGTH ( @colors ) ) IS NULL,
(
@itemid := NULL
AND @colors := NULL
),
'set'
) as Init,
IF( CAST(@itemid AS CHAR) = CAST(ItemID AS CHAR),
@colors := CONCAT( @colors, ',', ColorName ),
@colors := ColorName
) as Setting,
@itemid := CAST(ItemID AS CHAR),
ItemID,
@colors as ColorList
FROM (
SELECT i.ItemID,
i.ColorIDs,
c.Name
FROM Items i
INNER JOIN Colors c ON FIND_IN_SET(c.ColorID, i.ColorIDs)
ORDER BY i.ItemID
) combined
WHERE 1
ORDER BY ItemID,
LENGTH ( @colors ) DESC
) final
WHERE 1
GROUP BY ItemID
But, then of course it's incompatible with even some fairly recent versions of MySQL (since subselects were a recent addition).
SELECT
ItemID,
ColorName AS Colors
FROM Items
INNER JOIN Colors ON FIND_IN_SET(ColorID,ColorIDs)
;
--
-- Returns
--
+--------+-----------+
¦ ItemID ¦ Colors ¦
+--------+-----------+
¦ 1 ¦ White ¦
¦ 2 ¦ White ¦
¦ 1 ¦ Burgundy ¦
¦ 3 ¦ Burgundy ¦
¦ 3 ¦ SteelBlue ¦
+--------+-----------+
$sql = '
SELECT
ItemID,
ColorName AS Colors
FROM Items
INNER JOIN Colors ON FIND_IN_SET(ColorID,ColorIDs)
';
$tmp = array();
$rows = mysql_query($sql);
while ($row = mysql_fetch_array($rows)) {
if (isset($tmp[$row['ItemID']])) {
$tmp[$row['ItemID']] .= ',' . $row['Colors'];
} else {
$tmp[$row['ItemID']] = $row['Colors'];
}
}
print_r($tmp);
exit;
--
-- Returns
--
Array
(
[1] => White,Burgundy
[2] => White
[3] => Burgundy,SteelBlue
)