Re: SELECTING a sentence from a text field?

From: Date: Tue, 14 May 2002 11:30:48 +0000
Subject: Re: SELECTING a sentence from a text field?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-19176@lists.php.net to get a copy of this message
Here is what I use to select the first sentence from my text fields. $result = mysql_query("SELECT news_id, headline, substring_index(body, \".\" ,1) AS body FROM news",$db); Mike ::: Larry French ::: wrote:
Hello all. I have a MySQL field called 'description' that contains a paragraph of text. I want to pull out the first 30 words (or there about) from 'description'. This is what I came up with: "SELECT ID, name, LEFT(description,95), IF(enrolling = 1,'Currently enrolling','') FROM trials ORDER BY name ASC LIMIT $offset, $limit" Obviously this grabs the first 95 characters from 'description', but when I display 'description' the word at the end of the string (after the 95th charter) is often cut right in the middle. I know that I could select the whole field then use a regex to chop the string at the first '.' but I would rather have a SQL statement that only grabs the text I need. So how do I write a SQL statement that will grab the first 30 words without chopping the last word? Thanks for your time. larry.
-- Mike

« previous php.db (#19176) next »