php MySQL/SQL 检索文本字段的前 40 个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2080860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:55:44  来源:igfitidea点击:

MySQL/SQL retrieve first 40 characters of a text field?

phpsqlmysqlchop

提问by John

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

如何从 mysql db 表中检索文本字段,但不是整个文本,只有 40 个左右的字符。

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

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

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.

基本上我想要做的是显示前 x 个字符,然后让用户单击它以查看完整内容。

回答by jensgram

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

See the LEFT()function.

LEFT()功能。

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.

根据经验,您永远不应该在 PHP 中做 MySQL 可以为您做的事情。可以这样想:您不希望从数据库向请求应用程序传输任何超出严格必要的内容。



EDITIfyou'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 notto fetch the full text at once. (See comments and Veger's answer.)

编辑如果您要经常使用同一页面上的整个数据(即,没有中间请求),则没有理由立即获取全文。(见评论和维格的回答。)

回答by Cocowalla

SELECT LEFT(MY_COLUMN, 40) FROM MY_TABLE

Function in the MySQL reference manual:

MySQL参考手册中的函数:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left

回答by jeswin

try this...
SELECT LEFT(field name, 40) FROM table name WHERE conditionfor first 40 and
SELECT RIGHT(field name, 40) FROM table name WHERE conditionfor last 40

试试这个...
SELECT LEFT(field name, 40) FROM table name WHERE conditionfor first 40 and
SELECT RIGHT(field name, 40) FROM table name WHERE conditionfor last 40

回答by Veger

You could do this in SQL as the others shown already.

您可以在 SQL 中执行此操作,正如其他已显示的那样。

But, if you alsowant to show the full text if the user clicks on it, you also need to full text and it seems a waste to let the database send you the short and the full text. So you could grab the full text and write some code to show the short text and the full text when the user clicks on it.

但是,如果你想显示全文,则若用户点击,您还需要完整的文本和它似乎是一种浪费,让数据库发送给您的短期和全文。因此,您可以抓取全文并编写一些代码来在用户单击时显示短文本和全文。

$result = mysql_query('SELECT text FROM table');
$row = mysql_fetch_row($result);
echo '<div onclick="alert(\''.$row[0].'\');">'.substr($row[0], 0, 40).'</div>';

Ofcourse you could do something nicer when you click on it (instead of alert()). Also you could do some PHP checking now to see if the original is shorter than 40 characters and handle situations like this.

当然,当您点击它(而不是alert())时,您可以做一些更好的事情。此外,您现在可以进行一些 PHP 检查,以查看原始文件是否短于 40 个字符并处理此类情况。

回答by Lead Developer

Check this one as well,

也检查这个,

 mysql_query('SELECT LEFT('your text/fieldname', 40) FROM tablename');