2011-08-29 70 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