У меня есть таблица с комментариями и ответами. Я использую следующий запрос для получения парентид и оттуда получаю ответы (replyid), которые должны относиться к ним.
Проблема, с которой я сталкиваюсь, заключается в том, что когда я получаю данные, я могу организовать их по столбцу (replyid), но не могу организовать их по отметке времени. Или кажется, что postgresql полностью игнорирует второй порядок.
parentid | replyid | commentowner | username | commentbody | postcreation
------------------+------------------+---------------+----------+-------------------------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | bob@none.com | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | bob@none.com | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| 3X77KW5t9X43Psfp | bob@none.com | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | bob@none.com | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| BG7mm4cj7Q3Ujffs | bob@none.com | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| 3X77KW5t9X43Psfp | mike@none.com | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | mike@none.com | Mikey | Mike sent this!! | 2018-05-09 08:27:50.1
и таблица пользователей:
username | socialkey | emailaddress | bannerpic | avatarpic
----------+------------------------------------------------------------------+---------------+------------------------------------------------------------------------------+------------------------------------------------------------------------------------
Bobert | 9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a | bob@none.com | https://craftedin.co/static/images/user/headers/59d9bce7c33561.24964635.jpg | https://craftedin.co/static/images/user/avatars/thumbs/59159be40a8c76.26091804.jpg
Mikey | 3bcd6c5f811ba06aa49c2df9504fe5416f35702b78bcdc15ecbd5137fabdca59 | mike@none.com | https://i.pinimg.com/originals/29/85/16/298516524d3ed9eb35ea9aae0f111ab3.jpg | https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg
Если это поможет создать кодовый блок или что-то для этого, дайте мне знать, и я постараюсь, чтобы это произошло.
Вот запрос, который я выполняю:
with parents as (select distinct replyid from comments where commentowner = (select emailaddress from users where SocialKey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a')) and parentid IS NOT NULL LIMIT 5 ), socialkeys as (select distinct socialkey,emailaddress,avatarpic from users) select distinct comments.parentid, comments.replyid,comments.username, comments.commentbody,comments.postcreation from comments left join parents on parents.replyid = comments.replyid left join socialkeys on comments.commentowner = socialkeys.emailaddress where parents.replyid = comments.replyid order by replyid, postcreation;
Вот данные, которые я получаю:
parentid | replyid | username | commentbody | postcreation
------------------+------------------+----------+-------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
| 3X77KW5t9X43Psfp | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| 3X77KW5t9X43Psfp | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | Mikey | Mike sent this!! | 2018-05-09 08:27:50.140229
| 3X77KW5t9X43Psfp | Bobert | bob | 2018-06-28 14:22:07.131133
| 3X77KW5t9X43Psfp | Bobert | Oh so this really does work? | 2018-06-28 14:27:02.956613
| 3X77KW5t9X43Psfp | Bobert | this is a reply | 2018-06-28 17:41:49.071165
wpdcBFUft0r597IK | 4WMFSMSTSomjdoD9 | Bobert | this is parent comment number 5 I think the database is set to limit 5... | 2018-06-28 18:15:47.534429
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| BG7mm4cj7Q3Ujffs | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| BG7mm4cj7Q3Ujffs | Bobert | Another lame coment reply | 2018-05-19 12:55:56.090902
pw22kSb7MdA9Nx5G | MD3jA9znknV9c3p7 | Bobert | This is a new comment from bobbert on the new interfce | 2018-06-28 18:03:30.05851
bffV59nEQRg5YtP3 | tIZYNjkyzoHhKfza | Bobert | This is comment # 6... This should show up becasue it is the newest! of the newsts! | 2018-06-28 18:16:12.823547
Вот что я хочу получить. Все комментарии были собраны и упорядочены по replyid, но я хочу, чтобы они также соответствовали времени их поступления. Я надеюсь это имеет смысл. Я пытался все изобразить.
parentid | replyid | username | commentbody | postcreation
------------------+------------------+----------+-------------------------------------------------------------------------------------+----------------------------
gLKysUwsL5CVHRIR | 3X77KW5t9X43Psfp | Bobert | Some Lame Comment.... | 2018-05-08 13:21:43.049563
| 3X77KW5t9X43Psfp | Bobert | latest reply | 2018-05-08 13:22:37.535039
| 3X77KW5t9X43Psfp | Bobert | no I am the latest | 2018-05-08 13:22:52.868424
| 3X77KW5t9X43Psfp | Mikey | I am a response to to another parent | 2018-05-09 08:27:22.733211
| 3X77KW5t9X43Psfp | Mikey | Mike sent this!! | 2018-05-09 08:27:50.140229
| 3X77KW5t9X43Psfp | Bobert | bob | 2018-06-28 14:22:07.131133
| 3X77KW5t9X43Psfp | Bobert | Oh so this really does work? | 2018-06-28 14:27:02.956613
| 3X77KW5t9X43Psfp | Bobert | this is a reply | 2018-06-28 17:41:49.071165
yt1SLuFzRz1Nlri2 | BG7mm4cj7Q3Ujffs | Bobert | another parent comment | 2018-05-08 13:22:05.677257
| BG7mm4cj7Q3Ujffs | Bobert | I am a response to to another parent | 2018-05-08 13:23:42.167327
| BG7mm4cj7Q3Ujffs | Bobert | Another lame coment reply | 2018-05-19 12:55:56.090902
pw22kSb7MdA9Nx5G | MD3jA9znknV9c3p7 | Bobert | This is a new comment from bobbert on the new interfce | 2018-06-28 18:03:30.05851
wpdcBFUft0r597IK | 4WMFSMSTSomjdoD9 | Bobert | this is parent comment number 5 I think the database is set to limit 5... | 2018-06-28 18:15:47.534429
bffV59nEQRg5YtP3 | tIZYNjkyzoHhKfza | Bobert | This is comment # 6... This should show up becasue it is the newest! of the newsts! | 2018-06-28 18:16:12.823547
Я делаю это совершенно неправильно? Или я упускаю что-то простое?
Дайте мне знать, пожалуйста, спасибо.