我在SQL中有三个表:
video : ID, TITLE
join_video_clicked : ID_VIDEO, NB_VIEW
join_video_category : ID_VIDEO, ID_CATEGORY
我的实际SQL查询:
SELECT ID,TITLE,THUMB
FROM video
WHERE ID
IN (
SELECT * FROM
(
SELECT ID_VIDEO
FROM join_video_clicked
WHERE ID_VIDEO
IN
(
SELECT ID_VIDEO
FROM join_video_category
WHERE ID_CATEGORY = 8
)
ORDER BY CLICKED DESC
) AS TEMP
) LIMIT 0,24
解释1:
SELECT ID_VIDEO FROM join_video_clicked ORDER BY CLICKED DESC LIMIT 0,24
此查询可获得最多查看的24个
解释2:
SELECT ID_VIDEO FROM join_video_category WHERE ID_CATEGORY = 8
此查询从自定义类别
获取ID解释3:
SELECT ID,TITLE FROM video WHERE ID IN (!!!SUBQUERY!!!)
此查询使用子查询
查找id和title我希望您的帮助可以调整我的查询,以便按照正确的顺序从自定义类别中获取最常查看的24个视频,因为通过此查询,我在自定义类别中查看的视频次数最多,但不符合DESC的顺序。
答案 0 :(得分:0)
试试这个,
allowLargeResults
答案 1 :(得分:0)
您可以在两个表之间进行内部联接以获取该类别中的所有视频,并且在该查询中,您可以添加订单并限制:
SELECT jvclick.id_video
FROM join_video_clicked jvclick
JOIN join_video_category jvcat ON jvcat.id_video = jvclick.id_video
ORDER BY jvclick.clicked DESC
LIMIT 24;
答案 2 :(得分:0)
这样的事情应该这样做:
select
vid.id_video,
vid.title,
count(click.id_video) cnt
from
video_clicked click
join video vid on click.id_video = vid.id_video
join video_category cat on vid.id_video = cat.id_video
where
cat.id_category = 1
group by
vid.id_video,
vid.title
order by
cnt desc
;
完整示例:
use example;
/*
video : ID, TITLE
join_video_clicked : ID_VIDEO, NB_VIEW
join_video_category : ID_VIDEO, ID_CATEGORY
*/
drop table if exists video_clicked;
drop table if exists video_category;
drop table if exists video;
drop table if exists category;
create table category (
id_category int primary key,
name varchar(256)
);
create table video (
id_video int primary key,
title varchar(256)
);
create table video_category (
id_video int references video(id),
id_category int references category(id)
);
create table video_clicked (
id_video int references video(id),
nb_view varchar(256)
);
select
vid.id_video,
vid.title,
count(click.id_video) cnt
from
video_clicked click
join video vid on click.id_video = vid.id_video
join video_category cat on vid.id_video = cat.id_video
where
cat.id_category = 1
group by
vid.id_video,
vid.title
order by
cnt desc
;
答案 3 :(得分:-1)
SELECT v.ID,v.TITLE,v.THUMB FROM video v
JOIN join_video_clicked jvc ON v.ID=jvc.ID_VIDEO
JOIN join_video_category jvcat ON v.ID=jvcat.ID_VIDEO
WHERE jvcat.ID_CATEGORY = 8 ORDER BY jvc.CLICKED DESC LIMIT 24