Postgresql 按列和时间戳排序
Postgresql order by column and by timestamp
我有 table 是由评论和回复组成的。我正在使用以下查询获取 parentids 并从那里获取应该与它们相关的回复 (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
和用户 table:
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
我这样做完全错了吗?还是我漏掉了一些简单的东西?
请告诉我,谢谢。
从 2 个输出来看,parentid = wpdcBFUft0r597IK 的条目在您看来似乎顺序错误。该 parentid 只有一个条目,因此您的查询不关心您的 parentid 出现在哪个顺序。如果您确实希望按 parentid 的创建后排序,请尝试将您的查询更改为以下内容。
with parents
as (select replyid,
min(postcreation) postcreation
from comments
where commentowner = (select emailaddress
from users
where socialkey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a'))
and parentid is not null
group by replyid limit 5),
socialkeys
as (select distinct socialkey,
emailaddress,
avatarpic
from users)
select distinct parents.postcreation,
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 parents.postcreation,
comments.replyid,
comments.postcreation;
我将您的 parents CTE 更改为分组依据,这样您就可以在订单依据中使用 MIN POSTCREATION。我还用 table 名称限定了 ORDER BY 字段,以防在哪个 REPLYID 上排序不明确。
-HTH
我有 table 是由评论和回复组成的。我正在使用以下查询获取 parentids 并从那里获取应该与它们相关的回复 (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
和用户 table:
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
我这样做完全错了吗?还是我漏掉了一些简单的东西?
请告诉我,谢谢。
从 2 个输出来看,parentid = wpdcBFUft0r597IK 的条目在您看来似乎顺序错误。该 parentid 只有一个条目,因此您的查询不关心您的 parentid 出现在哪个顺序。如果您确实希望按 parentid 的创建后排序,请尝试将您的查询更改为以下内容。
with parents
as (select replyid,
min(postcreation) postcreation
from comments
where commentowner = (select emailaddress
from users
where socialkey = ('9784e946c53d44c975ee91625486d758fe630f176d44863080ec689ae3cd536a'))
and parentid is not null
group by replyid limit 5),
socialkeys
as (select distinct socialkey,
emailaddress,
avatarpic
from users)
select distinct parents.postcreation,
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 parents.postcreation,
comments.replyid,
comments.postcreation;
我将您的 parents CTE 更改为分组依据,这样您就可以在订单依据中使用 MIN POSTCREATION。我还用 table 名称限定了 ORDER BY 字段,以防在哪个 REPLYID 上排序不明确。
-HTH