Jump to content
php.lv forumi

mysql order by time


sandrulis
 Share

Recommended Posts

Sveiki, ir divas tabulas

 

posts:

id, title, time

1, nosaukums 1, 12345

2, nosaukums 2, 12346

3, nosaukums 3, 12347

4, nosaukums 4, 12348

 

comments:

id, post_id, comment, time

1, 2, comment 1, 12348

2, 2, comment 2, 12350

3, 3, comment 3, 12352

4, 1, comment 4, 12353

5, 3, comment 5, 12354

 

Kā man jānorādas mysql, lai rādītu rezultāti no posts, bet rādītu by time no abām tabulām, ja tipa postam ir jauns komentārs viņš viņu rāda kā pēdējo, kaut arī ir jaunāki posti.

"SELECT p.id,p.title,p.time FROM posts AS p LEFT JOIN comments AS c ON c.post_id = p.id GROUP BY p.id ORDER BY p.time DESC"

"ORDER BY p.time DESC" šajā vietā jāiemet kautkā ja (c.time > p.time ? c.time : p.time)

 

Ceru, ka sapratāt domu

Link to comment
Share on other sites

  • 3 months later...

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.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...