MySQL/SQL检索文本字段的前40个字符? [英] MySQL/SQL retrieve first 40 characters of a text field?

查看:177
本文介绍了MySQL/SQL检索文本字段的前40个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从mysql db表中检索文本字段,而不是整个文本,仅检索40个左右的字符.

How can I retrieve a text field from mysql db table, but not the entire text, just the few 40 or so characters.

这可以在sql中完成还是需要使用php来完成?

Can this be done in sql or do I need to do it using php?

基本上,我想做的是显示前x个字符,然后让用户单击以查看全部内容.

basically what I am trying to do is show the first x characters and then let the user click on that to view the full content.

推荐答案

SELECT LEFT(field, 40) AS excerpt FROM table(s) WHERE ...

请参见 LEFT() 函数

作为经验法则,您永远不要在PHP中执行MySQL可以为您做的事情.这样想:您不希望从数据库向请求应用程序传输超出绝对必要的内容.

As a rule of thumb, you should never do in PHP what MySQL can do for you. Think of it this way: You don't want to transmit anything more than strictly necessary from the DB to the requesting applications.

编辑 如果,您将比同一页面上使用整个数据 (即没有中间请求)的次数要多不,没有理由立即获取全文. (请参阅评论和蔬菜的答案.)

EDIT If you're going to use the entire data on the same page (i.e., with no intermediate request) more often than not, there's no reason not to fetch the full text at once. (See comments and Veger's answer.)

这篇关于MySQL/SQL检索文本字段的前40个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆