I am following the answer of this question: MySQL: is it possible to group_concat multiple rows?
But I struggle with the MySql error:
#1054 - Unknown column 'CM_Cocktail.id' in 'field list'
.
When I delete this CM_Cocktail.id stuff the next error apperars: #1054 - Unknown column 'CM_Zutat.name' in 'field list'
. I tried to use aliases but it gone worse... Maybe I am blind.
SELECT
CM_Cocktail.id, CM_Cocktail.name,
GROUP_CONCAT(CM_Zutat.name SEPARATOR ', ')
FROM (
SELECT CM_Cocktail.id, CM_Cocktail.name AS cname, CM_Zutat.name
FROM CM_Cocktail, CM_CocktailHatZutat, CM_Zutat
WHERE CM_Cocktail.id=CM_CocktailHatZutat.cocktail_id
AND CM_CocktailHatZutat.zutat_id=CM_Zutat.id
) group_attr
GROUP BY CM_Cocktail.id;
CREATE TABLE IF NOT EXISTS `CM_Cocktail` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `CM_CocktailHatZutat` (
`cocktail_id` int(3) NOT NULL,
`zutat_id` int(3) NOT NULL,
`zutat_menge` int(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `CM_Zutat` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
To fix the error above, simply add a quotation mark around the value. You can use both single quotes or double quotes as shown below: INSERT INTO users(username, display_name) VALUES ("jackolantern", 'Jack'); Now the INSERT statement should run without any error.
MySQL error code 1054 occurs if we forget to add single quotes while inserting a varchar value or due to any missing column.
To rename a column in MySQL the following syntax is used: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; This command is used to change the name of a column to a new column name.
Use ADD to add new columns to a table, and DROP to remove existing columns. DROP col_name is a MySQL extension to standard SQL. To add a column at a specific position within a table row, use FIRST or AFTER col_name . The default is to add the column last.
You need to define column alias in your sub query and than use alias as parent fields as this example:
SELECT
CocktailID, cname,
GROUP_CONCAT(ZName SEPARATOR ', ')
FROM (
SELECT CM_Cocktail.id as CocktailID, CM_Cocktail.name AS cname, CM_Zutat.name as ZName
FROM CM_Cocktail, CM_CocktailHatZutat, CM_Zutat
WHERE CM_Cocktail.id=CM_CocktailHatZutat.cocktail_id
AND CM_CocktailHatZutat.zutat_id=CM_Zutat.id
) group_attr
GROUP BY CocktailID;
plz try this
SELECT
id, name,
GROUP_CONCAT(CM_Zutat.name SEPARATOR ', ')
FROM (
SELECT CM_Cocktail.id, CM_Cocktail.name AS cname, CM_Zutat.name
FROM CM_Cocktail, CM_CocktailHatZutat, CM_Zutat
WHERE CM_Cocktail.id=CM_CocktailHatZutat.cocktail_id
AND CM_CocktailHatZutat.zutat_id=CM_Zutat.id
) group_attr
GROUP BY id;
because you use subquery
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With