简体   繁体   English

使用别名在MySQL中带有空格的AS查询

[英]Query with AS with aliaces with spaces in mysql

i have this challenge on tree house 我对树屋有这个挑战

We're back in our e-commerce database. 我们回到了电子商务数据库。 There's a products table with the columns id, name, description and price. 有一个产品表,其中包含ID,名称,描述和价格列。 Can you retrieve both the name and description aliased as "Product Name" and "Product Description". 您是否可以检索别名和别名为“产品名称”和“产品描述”的名称和描述。

And my code is 我的代码是

SELECT name  AS 'Product Name' , description  AS ' Product Description' FROM  products;

But is says an error.. 但这是一个错误。

Can you please tell me where i am doing it wrong? 您能告诉我我做错了什么吗? thanks 谢谢

The columns you selected were named Product Name, Product Description and not 'Product Name' and 'Product Description'. 您选择的列名为“产品名称”,“产品描述”,而不是“产品名称”和“产品描述”。 在此处输入图片说明

As the exercise you posted says, the column alias names must be without ' . 如您所发布的练习所述,列别名名称必须不包含' Since the aliases have spaces in them, you would need to enclose the names with [] , not with ' . 由于别名中包含空格,因此您需要将名称用[]而不是'

Try this: 尝试这个:

SELECT name AS [Product Name], description AS [Product Description] FROM  products;

It seems the task 1 is waiting for double quotes but you're using single quotes. 似乎任务1正在等待双引号,但您正在使用单引号。

Anyway, you can use single quotes, it's correct. 无论如何,您可以使用单引号,这是正确的。

SQL identifiers must be in backticks when using MySQL. 使用MySQL时,SQL标识符必须放在反引号中。 You put single quotes around the column names. 您在列名两边加上单引号。

And by the way: You have an extra space in ' Product Desciption' 顺便说一句:“产品说明”中有额外的空间

Backticks work well for Mysql. 反引号对于Mysql效果很好。 Try this 尝试这个

SELECT name  AS `Product Name` , description  AS `Product Description` FROM  products;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM