我有以下查询,它以随机顺序从某些类别中检索4个广告.
目前,如果用户有超过1个广告,则可能会检索所有这些广告 - 我需要对其进行限制,以便每个用户只展示1个广告.
这可能在同一个查询中实现吗?
SELECT a.advert_id, a.title, a.url, a.user_id,
FLOOR(1 + RAND() * x.m_id) 'rand_ind'
FROM adverts AS a
INNER JOIN advert_categories AS ac
ON a.advert_id = ac.advert_id,
(
SELECT MAX(t.advert_id) - 1 'm_id'
FROM adverts t
) x
WHERE ac.category_id IN
(
SELECT category_id
FROM website_categories
WHERE website_id = '8'
)
AND a.advert_type = 'text'
GROUP BY a.advert_id
ORDER BY rand_ind
LIMIT 4
Run Code Online (Sandbox Code Playgroud)
测试架构和数据
create table adverts (
advert_id int primary key, title varchar(20), url varchar(20), user_id int, advert_type varchar(10))
;
create table advert_categories (
advert_id int, category_id int, primary key(category_id, advert_id))
;
create table website_categories (
website_id int, category_id int, primary key(website_id, category_id))
;
insert website_categories values
(8,1),(8,3),(8,5),
(1,1),(2,3),(4,5)
;
insert adverts (advert_id, title, user_id) values
(1, 'StackExchange', 1),
(2, 'StackOverflow', 1),
(3, 'SuperUser', 1),
(4, 'ServerFault', 1),
(5, 'Programming', 1),
(6, 'C#', 2),
(7, 'Java', 2),
(8, 'Python', 2),
(9, 'Perl', 2),
(10, 'Google', 3)
;
update adverts set advert_type = 'text'
;
insert advert_categories values
(1,1),(1,3),
(2,3),(2,4),
(3,1),(3,2),(3,3),(3,4),
(4,1),
(5,4),
(6,1),(6,4),
(7,2),
(8,1),
(9,3),
(10,3),(10,5)
;
Run Code Online (Sandbox Code Playgroud)
数据属性
此查询将 3 个表连接在一起(请注意,id 1、3 和 10 各出现两次)
select *
from website_categories wc
inner join advert_categories ac on wc.category_id = ac.category_id
inner join adverts a on a.advert_id = ac.advert_id and a.advert_type = 'text'
where wc.website_id='8'
order by a.advert_id
Run Code Online (Sandbox Code Playgroud)
为了使每个网站仅显示一次,这是显示所有符合条件的广告的核心查询,每个广告仅显示一次
select *
from adverts a
where a.advert_type = 'text'
and exists (
select *
from website_categories wc
inner join advert_categories ac on wc.category_id = ac.category_id
where wc.website_id='8'
and a.advert_id = ac.advert_id)
Run Code Online (Sandbox Code Playgroud)
下一个查询检索所有要显示的 advert_id
select advert_id, user_id
from (
select
advert_id, user_id,
@r := @r + 1 r
from (select @r:=0) r
cross join
(
# core query -- vvv
select a.advert_id, a.user_id
from adverts a
where a.advert_type = 'text'
and exists (
select *
from website_categories wc
inner join advert_categories ac on wc.category_id = ac.category_id
where wc.website_id='8'
and a.advert_id = ac.advert_id)
# core query -- ^^^
order by rand()
) EligibleAdsAndUserIDs
) RowNumbered
group by user_id
order by r
limit 2
Run Code Online (Sandbox Code Playgroud)
此查询有 3 个级别
EligibleAdsAndUserIDs:核心查询,使用随机排序order by rand()RowNumbered:使用 MySQL 副作用 @variables 添加到核心查询的行号as numbered最外面的查询强制 mysql在内部查询中随机收集行,并group by user_id导致它只保留每个 user_id 的第一行。limit 2一旦遇到两个不同的 user_id,查询就会停止。这是最终的查询,它从上一个查询中获取 advert_id 并将其连接回表adverts以检索所需的列。
注意:第 (2) 点之所以有效,是因为您拥有的广告越多,您就越有可能在行编号子查询中获得最高排名
select a.advert_id, a.title, a.url, a.user_id
from
(
select advert_id
from (
select
advert_id, user_id,
@r := @r + 1 r
from (select @r:=0) r
cross join
(
# core query -- vvv
select a.advert_id, a.user_id
from adverts a
where a.advert_type = 'text'
and exists (
select *
from website_categories wc
inner join advert_categories ac on wc.category_id = ac.category_id
where wc.website_id='8'
and a.advert_id = ac.advert_id)
# core query -- ^^^
order by rand()
) EligibleAdsAndUserIDs
) RowNumbered
group by user_id
order by r
limit 2
) Top2
inner join adverts a on a.advert_id = Top2.advert_id;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
429 次 |
| 最近记录: |