Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a SQL column title via query

Tags:

sql

mysql

I have the following query:

SELECT product_description.name, product.quantity,product.price,product_option_value_description.name,product_option_value.quantity
FROM product
INNER JOIN product_description
ON product.product_id=product_description.product_id
INNER JOIN product_option_value_description
ON product.product_id=product_option_value_description.product_id
INNER JOIN product_option_value
ON product.product_id=product_option_value.product_id
ORDER BY product_description.name 

How could I change the title for product_option_value_description.name as I would like to name this option.

like image 336
Jess McKenzie Avatar asked Jul 25 '11 06:07

Jess McKenzie


People also ask

How can I change the column name in SQL query?

1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.


3 Answers

Use an alias like so:

product_option_value_description.name AS `Option`

If you want to change the column's name, not only for this query but in general use ALTER TABLE

ALTER TABLE product_option_value_description CHANGE name newname DATATYPE;
like image 139
Jacob Avatar answered Sep 22 '22 01:09

Jacob


Just write product_option_value_description.name AS Name to create the alias "Name" for this column.

like image 40
JK. Avatar answered Sep 24 '22 01:09

JK.


Use as

For example:

SELECT product_description.name as 'ProdName', product.quantity,product.price,product_option_value_description.name as 'ProdDesc',product_option_value.quantity FROM product
like image 40
NightWolf Avatar answered Sep 24 '22 01:09

NightWolf