mysql - 使用 MySQL 内连接进行搜索和排序
问题描述
我想我无法正确解释我的问题。我想用一张图给你解释一下。
图片 1 在第一张图片中,您可以在趋势部分看到主题标签。搜索这些主题标签的最高总数,并检查日期是否已过。如果有效数据可用,则采用前 5 个主题标签。
图2 第二张图,检查hashtag中的帖子是否在帖子中,如果有,取最早的日期值,LIMIT设置为1,oyuncular表中的id值与sid匹配。因此,可以访问共享人的姓名。
我的英语有点糟糕,我希望我能解释清楚。
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
social_post.sid,
social_post.tarih,
social_post.post,
oyuncular.id,
oyuncular.isim
FROM
social_trend
INNER JOIN
social_post
ON
social_post.post LIKE '%social_trend.hashtag%' ORDER BY social_post.tarih LIMIT 1
INNER JOIN
oyuncular
ON
oyuncular.id = social_post.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
解决方案
您应该使用 sibquery
并在 subqiery 和 social_trend 之间添加适当的连接
(我假设同时唱 sid)
SELECT
social_trend.hashtag,
social_trend.total,
social_trend.tarih,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.*
from social_post
INNER JOIN social_trend ON social_post.post LIKE concat('%',social_trend.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 1
) t
INNER JOIN social_trend ON social_trend.hashtag= t.post
INNER JOIN oyuncular ON oyuncular.id = t.sid
WHERE
social_trend.tarih > UNIX_TIMESTAMP() ORDER BY social_trend.total DESC LIMIT 5
但是看着你的新解释和 img 似乎你需要
SELECT
t.hashtag,
t.total,
t.tarih_trend,
t.sid,
t.tarih,
t.post,
oyuncular.id,
oyuncular.isim
FROM (
select social_post.sid
, social_post.tarih
, social_post.post
, st.hashtag
, st.total
, st.tarih tarih_trend
from social_post
INNER JOIN (
select * from social_trend
WHERE social_trend.tarih > UNIX_TIMESTAMP()
order by total DESC LIMIT 5
) st ON social_post.post LIKE concat('%',st.hashtag,'%' )
ORDER BY social_post.tarih LIMIT 5
) t
INNER JOIN oyuncular ON oyuncular.id = t.sid
推荐阅读
- ios - (iOS) viewport-fit=cover 元标记不能完美地与导航栏配合使用
- python - Sqlite“OperationalError:靠近“05”:语法错误”
- database - SQLite:从路径列表创建目录结构表
- reactjs - 异步等待未通过 onupload 进度回调捕获 401 axios
- nltk - 使用 NLTK 以三列格式读取自定义语料库以进行 pos 标记
- java - 在来自 Micronaut 休息端点的 JSON 中包含 null 和空属性
- java - 当您想要链接构造函数时,如何处理 IO 句柄?
- numpy - 在 Numpy 数组的数据集上使用 Keras 模型?
- php - FPDF MultiCell Long Text - 如何设置开始的页面
- c# - 将 Select Query (SQL Server) 的结果保存到 Excel 的工作表中