2013-06-28 48 views
-1

我有一個模塊生成一個csv文件,將數據提供給第三方,但希望修改它以供其他公司使用。他們想要的csv的列名與當前模塊中設置的名稱不同,例如Product Name而不是product__name。在MySQL查詢中使用'as'

原國防部的SQL的一個例子是:

$query = 'select 
    p.products_id as uuid, 
    t.type_name as product__type, 
    d.products_name as product__name, 
    d.products_description as product__description, 
    d.products_url as product__url, 
    d.products_viewed as product__viewed_count, 
    p.products_quantity as product__quantity, 
    p.products_model as product__model, 
    p.products_image as product__image 
    ..... 

我試圖改變d.products_name爲product__name,以d.products_name作爲產品名稱,但它給了我一個SQL錯誤。

有沒有任何簡單的解決方案,而不必重寫整個模塊?

+1

對於大多數人來說,它是非常狡猾的附上別名,但downvote? – DanFromGermany

回答

0

試試這個:

d.products_name as 'Product Name', 

當你想與間距的別名,你必須把它作爲一個字符串。

d.products_name as 'Product Name', 
1

可以使用反引號括您的別名。

d.products_name as `Product Name` 

這對於輸出具有空格的mysql關鍵字或列名稱的列名非常有用。

+0

你也可以使用。 – Strawberry