I am not so into database and I have the following problem working on a MySql database.
I have a table like this:
CREATE TABLE CommodityName (
id BigInt(20) NOT NULL AUTO_INCREMENT,
commodity_details_id BigInt(20) NOT NULL,
language_id BigInt(20) NOT NULL,
commodity_name VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
description Text CHARACTER SET latin1 COLLATE latin1_swedish_ci,
PRIMARY KEY (
id
)
) ENGINE=InnoDB AUTO_INCREMENT=87 ROW_FORMAT=DYNAMIC DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
ALTER TABLE CommodityName COMMENT = '';
The records in this table represents commodities information in different languages.
The commodity is specified by the univocally by the commodity_details_id (that is a FK of another table).
A specific commodity can have multiple record into this CommodityName table (I have a record for each translation in a specific language, represented by the language_id field). For example I have these 2 records:
id commodity_details_id language_id commodity_name description
-------------------------------------------------------------------------------------------------------
1 1 1 Rice Asia Rice Asia
16 1 3 Umuceli Asia Umuceli Asia
that represents the same commodities information (both having commodity_details_id=1) but in 2 languages (English and Kinirwanda language). In theory I can have also other languages, so I can have n records for a specific commodity.
I need to create a query where for a specified commodity (WHERE commodity_details_id=1) it returns all the commodity_name fields of the retrieved record as column of a single record.
Referring to the previous example something like this:
commodity_details_id commodity_name_EN commodity_name_RWA commodity_name_FR
----------------------------------------------------------------------------------------------------------------
1 Rice Asia Umuceli Asia null
How can I implement a query like this that from multiple records transform a part of the information in each records as a new column in the new query?