Jump to content

I dont know how to select distinct in here


npsari

Recommended Posts

Hi  :)

 

q = "SELECT DISTINCT t.topic_id, t.topic_name, t.topic_category_id, t.topic_date, t.topic_time, u.Image1, m.category_name, u.ID, r.reply_date, r.reply_time
FROM topic t
LEFT JOIN profile u
ON u.ID = t.topic_who
LEFT JOIN category m
ON t.topic_category_id = m.category_id 
LEFT JOIN reply r
ON t.topic_id = r.reply_topic_id
ORDER BY t.topic_date DESC, t.topic_time DESC LIMIT 9;";

 

The above code does not display distinct topic_id

it gets repeated many times

Can you tell me how to fix it please

Link to comment
Share on other sites

I added the GROUP BY, it worked and thinsga re not repeated, but now, the topics are not ordered by the latest reply (r.reply_date, r.reply_time)

 

$q = "SELECT DISTINCT t.topic_id, t.topic_name, t.topic_category_id, t.topic_date, t.topic_time, u.Image1, m.category_name, u.ID, r.reply_date, r.reply_time

FROM topic t

LEFT JOIN profile u

ON u.ID = t.topic_who

LEFT JOIN category m

ON t.topic_category_id = m.category_id

LEFT JOIN reply r

ON t.topic_id = r.reply_topic_id

GROUP BY t.topic_id

ORDER BY r.reply_date DESC, r.reply_time DESC LIMIT 9;";

 

Any other hint plz

Link to comment
Share on other sites

Not tested - I don't have your data

 

<?php
$q = "SELECT t.topic_id, t.topic_name, t.topic_category_id, t.topic_date, t.topic_time, 
    u.Image1, m.category_name, u.ID, 
    GROUP_CONCAT(r.reply_date, ' ', r.reply_time ORDER BY r.reply_date DESC,r.reply_time DESC SEPARATOR '<br/>') as replies
FROM topic t
LEFT JOIN profile u
ON u.ID = t.topic_who
LEFT JOIN category m
ON t.topic_category_id = m.category_id
LEFT JOIN reply r
ON t.topic_id = r.reply_topic_id
GROUP BY t.topic_id";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.