开发者

mysql query o retrieve records based on the following conditions

Please help me with a mysql query, using PHP to access databse and process data:

I have variables

1) $content_number_of_characters
2) $number_of_comments

and mysql table name: udjacomments

fields of table udjacomments: id, full_name, content, comment_url, is_published, and开发者_如何转开发 time_added

I would like to retrieve each entry into an array. For content, I would like to only get the content up to the character $content_number_of_characters (In other words, if $content_number_of_characters is 120, and content is 300 characters, I only want the first 120 characters).

(I do not know how I could use the mysql LEFT(str, len) into the query)

Then, I only want number of records ( $number_of_comments ) in a descending order from id and if field is_published == 1

Example:

I have records with id: 50, 51, 52, 53, 54, 55, 56 all records have is_published == 1, with the exception of record with id 55; record id 55 is_published == 0. Last, variable $number_of_comments == 3

the query would retrieve records 56, 54, and 53

Thank you,


  1. Number of comments:

    $query = "SELECT * FROM udjacomments where is_published = 1 order by id desc limit $number_of_comments"

  2. 120 chars

    $query = "select id, if(CHAR_LENGTH(content) > $content_number_of_characters, SUBSTR(content, 1, $content_number_of_characters), content) FROM udjacomments";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜