2011-08-29 65 views
0

请帮我一个MySQL查询,使用PHP访问DATABSE和处理数据:MySQL查询Ø检索基于以下条件的记录

我有变量

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

谢谢

回答

0
  1. 评论数:

    $查询= “SELECT * FROM udjacomments 其中is_published = 1 ORDER BY ID DESC LIMIT $ NUMBER_OF_COMMENTS”

  2. 120个字符

    $查询= “选择ID, 如果(CHAR_LENGTH(内容)> $ content_number_of_characters,SUBSTR(内容1,$ content_number_of_characters),内容) FROM udjacomments”;

+0

THANK YOU可以在查询是:$查询=“选择ID,FULL_NAME,如果(CHAR_LENGTH(内容)> $ content_number_of_characters,SUBSTR(内容,1,$ content_number_of_characters),内容),comment_url,is_published和time_added FROM udjacomments where is_published = 1 order by id desc limit $ number_of_comments“换句话说,我可以将两个查询合并为一个...我正在测试 – IberoMedia

+0

THANX我使用了您的建议,并合并了查询。这是我的最后一个查询:\t $ query =“SELECT id,full_name,if(CHAR_LENGTH(content)>”。$ content_number_of_characters。“,SUBSTR(content,1,”。content_number_of_characters。“),content),comment_url,time_added FROM udjacomments where is_published = 1 order by ID DESC limit“。$ number_of_comments; – IberoMedia