Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql: unknown column id in 'field list'

Tags:

sql

php

mysql

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 ;
like image 643
andre-3000 Avatar asked Feb 19 '16 10:02

andre-3000


People also ask

How do you fix Unknown column in field list?

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.

What is error code 1054 in MySQL?

MySQL error code 1054 occurs if we forget to add single quotes while inserting a varchar value or due to any missing column.

How do I change column headings in MySQL?

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.

How do I add a column to a table in MySQL?

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.


2 Answers

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;
like image 93
devpro Avatar answered Oct 25 '22 20:10

devpro


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

like image 37
toannh Avatar answered Oct 25 '22 20:10

toannh