mysql查询根据以下条件检索记录

时间:2011-08-29 05:53:11

标签: mysql

请帮我处理mysql查询,使用PHP访问数据库和处理数据:

我有变量

1) $content_number_of_characters
2) $number_of_comments

和mysql表名:udjacomments

表格udjacomments的字段:id, full_name, content, comment_url, is_published, and time_added

我想将每个条目检索到一个数组中。对于内容,我想只获得内容$content_number_of_characters的内容(换句话说,如果$ content_number_of_characters为120,内容为300个字符,我只需要前120个字符)。

(我不知道如何在查询中使用mysql LEFT(str,len))

然后,我只想从id开始按降序排列记录($number_of_comments),如果字段is_published == 1

示例:

我的记录ID为:50,51,52,53,54,55,56 所有记录都有is_published == 1,但id为55的记录除外;记录ID 55 is_published == 0.最后,变量$number_of_comments == 3

查询将检索记录56,54和53

谢谢,

1 个答案:

答案 0 :(得分:0)

  1. 评论数量:

    $ query =“SELECT * FROM udjacomments           其中is_published = 1个ID by id desc limit $ number_of_comments“

  2. 120个字符

    $ query =“select id,          if(CHAR_LENGTH(content)> $ content_number_of_characters,SUBSTR(content,1,$ content_number_of_characters),content)          来自udjacomments“;

相关问题