Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving only part of the record in database

Tags:

sql

php

mysql

I have a database which contains some topics each with a title and some description. In the title page, I have to display all the titles and a few lines of description for that topic. When a user clicks on one topic then he is taken to a page where he can find the entire description. Exactly like stackoverflow site, where description of each question is displayed in short.

I was wondering is there a way we can retrieve such short description from the database directly or should the front end parse the retrieved record and truncate it accordingly? Is there a way to query for a short version of the record from the database directly. I am using MySql as my database and php to display web pages.

like image 293
sasidhar Avatar asked Dec 28 '25 16:12

sasidhar


2 Answers

SELECT SUBSTRING(`title`, 0, 100) FROM `topics`
like image 178
Alex Pliutau Avatar answered Dec 30 '25 06:12

Alex Pliutau


I would say the answer is found in this post: SQL Server substring breaking on words, not characters

You wouldn't want to just limit by a string length, since then you might cut off a word right in the middle. Instead, you would want to set your max length and then move back to the place where there is a space.

Another option would be to run this once and then update a field in the table that stores the truncated text. That way you could limit your extra SQL calls and yet still have the nicely-formatted summary text.

like image 36
IAmTimCorey Avatar answered Dec 30 '25 08:12

IAmTimCorey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!