MYSQL日期时间为varchar以及如何仅显示年份

时间:2014-03-20 10:01:30

标签: mysql sql datetime

我有这个应用程序,Musicalbums的发布者以Varchar的形式保存到MySQL数据库中(我对此没有影响)所以它显示的日期如(2014-09-11)。我怎样才能让Releasedate只显示(2014)?我是否必须将其转换为DATETIME

我的MySQl查询如下所示:

    SELECT products.pro_id, 
           products.title AS album_title, 
           products.genre AS album_genre, 
           products.coverarttoenailurl AS cover_image, 
           products.ccws_pro_id AS product_upc, 
           track_title, 
           track_duration,  
           album_id, 
           physicalReleaseDate, 
           digitalReleaseDate 
   FROM products 
   INNER JOIN track_albumn_information 
   ON products.ccws_pro_upc = track_albumn_information.product_upc 
   AND track_albumn_information.artist_id ='".$artist_id."' 
   ORDER BY physicalReleaseDate, digitalReleaseDate DESC

相关字段为physicalReleaseDatedigitalReleaseDate

请帮助任何人......

5 个答案:

答案 0 :(得分:1)

试试这个...使用mysql的Year函数进行ref。 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_year

SELECT products.pro_id,products.title AS album_title, products.genre AS album_genre, products.coverarttoenailurl AS cover_image, products.ccws_pro_id 
                AS product_upc, track_title, track_duration, album_id, Year(physicalReleaseDate) , year(digitalReleaseDate) FROM products INNER JOIN track_albumn_information 
                ON products.ccws_pro_upc = track_albumn_information.product_upc AND track_albumn_information.artist_id ='".$artist_id."' ORDER BY physicalReleaseDate, digitalReleaseDate DESC

答案 1 :(得分:0)

由于它是varchar列,因此您使用LEFT函数

LEFT(physicalReleaseDate,4)

答案 2 :(得分:0)

您可以使用LEFT()从字符串的左侧返回指定数量的字符,即

LEFT(digitalReleaseDate,4)

For reference.

答案 3 :(得分:0)

YEAR(STR_TO_DATE(datecolumn,'%Y-%m-%d'))

答案 4 :(得分:-1)

SUBSTR(physicalReleaseDate, 1, 4)
SUBSTR(digitalReleaseDate, 1, 4)