MySQL,获取一行在多行集合中的位置
MySQL, get the position of a row in a set for multiple row
我有这个查询计算 publication
在一组出版物中的位置(这里命名为 community
),根据 effective_publishing_date
:
SELECT p.publication_id
, p.name publication_name
, IF(p.scheduled_at is not null, p.scheduled_at, p.created_at) effective_publishing_date
, @current_rank := @current_rank + 1 publication_rank
FROM publications p
JOIN (SELECT @current_rank := 0) r
WHERE p.community_id = 8513
ORDER
BY effective_publishing_date ASC;
此结果为:
[![在此处输入图片描述][1]][1]
现在我有一个 feed_item
的列表,其中每个 community_id
和 publication_id
作为属性,我想为每个 feed_item
获取关联的 publication_rank
.
例如,如果我有一个 publication_item
,其中 publication_id
= 18 且 community_id
= 2,我想要 publication_id
的 publication_rank
#在 community_id
#2 的所有出版物中排名第 18。我没有在一个查询(或子查询等)中成功获得它。
提前致谢,
这是我最终找到的两个解决方案。感谢@Barmar!
- 仅限连接:
SELECT
g1.community_id,
g1.publication_name,
g1.publication_name,
g1.publication_id,
COUNT(*) AS rank
FROM
(
SELECT
publications.publication_id as publication_id,
publications.name as publication_name,
publications.community_id as community_id,
communities.name as community_name,
IF(
publications.scheduled_at is not null,
publications.scheduled_at, publications.created_at
) as effective_publishing_date
FROM
feed_items
JOIN publications ON feed_items.publication_id = publications.publication_id
JOIN communities ON publications.community_id = communities.community_id
WHERE
feed_items.user_id = 489387
) AS g1
JOIN (
SELECT
publications.publication_id as publication_id,
publications.community_id as community_id,
IF(
publications.scheduled_at is not null,
publications.scheduled_at, publications.created_at
) as effective_publishing_date
FROM
feed_items
JOIN publications ON feed_items.publication_id = publications.publication_id
WHERE
feed_items.user_id = 489387
) AS g2 ON (
g2.effective_publishing_date, g2.publication_id
) <= (
g1.effective_publishing_date, g1.publication_id
)
AND g1.community_id = g2.community_id
GROUP BY
g1.publication_id,
g1.community_id,
g1.effective_publishing_date
ORDER BY
g1.community_id,
rank ASC;
- 有 SQL 个变量
SELECT data_table.publication_id, data_table.publication_name, data_table.community_id, data_table.effective_publishing_date,
@publication := IF(@community <> data_table.community_id, concat(left(@community := data_table.community_id, 0), 0), @publication+1) AS rank
FROM
(SELECT @publication:= -1) p,
(SELECT @community:= -1) c,
(SELECT
publication.name as publication_name,
publication.community_id as community_id,
feed_item.publication_id as publication_id,
IF(publication.scheduled_at is not null, publication.scheduled_at, publication.created_at) as effective_publishing_date
FROM feed_items feed_item
JOIN publications publication ON feed_item.publication_id = publication.publication_id
WHERE feed_item.user_id = 489387
ORDER BY publication.community_id, effective_publishing_date ASC
) data_table;
我有这个查询计算 publication
在一组出版物中的位置(这里命名为 community
),根据 effective_publishing_date
:
SELECT p.publication_id
, p.name publication_name
, IF(p.scheduled_at is not null, p.scheduled_at, p.created_at) effective_publishing_date
, @current_rank := @current_rank + 1 publication_rank
FROM publications p
JOIN (SELECT @current_rank := 0) r
WHERE p.community_id = 8513
ORDER
BY effective_publishing_date ASC;
此结果为:
[![在此处输入图片描述][1]][1]
现在我有一个 feed_item
的列表,其中每个 community_id
和 publication_id
作为属性,我想为每个 feed_item
获取关联的 publication_rank
.
例如,如果我有一个 publication_item
,其中 publication_id
= 18 且 community_id
= 2,我想要 publication_id
的 publication_rank
#在 community_id
#2 的所有出版物中排名第 18。我没有在一个查询(或子查询等)中成功获得它。
提前致谢,
这是我最终找到的两个解决方案。感谢@Barmar!
- 仅限连接:
SELECT
g1.community_id,
g1.publication_name,
g1.publication_name,
g1.publication_id,
COUNT(*) AS rank
FROM
(
SELECT
publications.publication_id as publication_id,
publications.name as publication_name,
publications.community_id as community_id,
communities.name as community_name,
IF(
publications.scheduled_at is not null,
publications.scheduled_at, publications.created_at
) as effective_publishing_date
FROM
feed_items
JOIN publications ON feed_items.publication_id = publications.publication_id
JOIN communities ON publications.community_id = communities.community_id
WHERE
feed_items.user_id = 489387
) AS g1
JOIN (
SELECT
publications.publication_id as publication_id,
publications.community_id as community_id,
IF(
publications.scheduled_at is not null,
publications.scheduled_at, publications.created_at
) as effective_publishing_date
FROM
feed_items
JOIN publications ON feed_items.publication_id = publications.publication_id
WHERE
feed_items.user_id = 489387
) AS g2 ON (
g2.effective_publishing_date, g2.publication_id
) <= (
g1.effective_publishing_date, g1.publication_id
)
AND g1.community_id = g2.community_id
GROUP BY
g1.publication_id,
g1.community_id,
g1.effective_publishing_date
ORDER BY
g1.community_id,
rank ASC;
- 有 SQL 个变量
SELECT data_table.publication_id, data_table.publication_name, data_table.community_id, data_table.effective_publishing_date,
@publication := IF(@community <> data_table.community_id, concat(left(@community := data_table.community_id, 0), 0), @publication+1) AS rank
FROM
(SELECT @publication:= -1) p,
(SELECT @community:= -1) c,
(SELECT
publication.name as publication_name,
publication.community_id as community_id,
feed_item.publication_id as publication_id,
IF(publication.scheduled_at is not null, publication.scheduled_at, publication.created_at) as effective_publishing_date
FROM feed_items feed_item
JOIN publications publication ON feed_item.publication_id = publication.publication_id
WHERE feed_item.user_id = 489387
ORDER BY publication.community_id, effective_publishing_date ASC
) data_table;