选择查询限制列字符不超过100

时间:2016-03-31 08:12:45

标签: mysql

目前,我有这个选择查询,其中从数据库中选择电影详细信息。现在我尝试将description列限制为仅选择100个字符用于显示目的。有没有办法做到这一点。提前谢谢。

这是我当前的SQL查询:

"SELECT title, description, release_year, rating, f.last_update, c.name FROM nfc_film f 
 INNER JOIN nfc_film_category fc ON f.film_id = fc.film_id
 INNER JOIN nfc_category c ON c.category_id = fc.category_id
 ORDER BY title";

2 个答案:

答案 0 :(得分:0)

您可以使用substring函数,如下所示:

SUBSTRING(str,pos,len)

str:你要剪切的字符串。 pos:从你想要切割的起始位置。 len:字符长度,直到需要剪切的位置。

希望有所帮助

SELECT title, 
   CASE 
       WHEN LENGTH(description) > 100 THEN SUBSTRING(description, 1, 100) 
       ELSE description 
   END AS 'description', 
   release_year, 
   rating, 
   f.last_update, 
   c.name
FROM nfc_film f
INNER JOIN nfc_film_category fc ON f.film_id = fc.film_id 
INNER JOIN nfc_category c ON c.category_id = fc.category_id 
ORDER BY title;

答案 1 :(得分:0)

利用LEFTIF功能实现:

SELECT title,
    CONCAT(LEFT(description,100), 
           IF(length(description) > 100, '...', '')) as description,
    release_year, rating, f.last_update, c.name FROM nfc_film f 
INNER JOIN nfc_film_category fc ON f.film_id = fc.film_id
INNER JOIN nfc_category c ON c.category_id = fc.category_id
ORDER BY title