使用MySQL内连接进行搜索和排序



我想我不能正确地解释我的问题。我想用一张图片给你解释一下。

图片1在第一张图中,你可以看到趋势部分的标签。这些标签将搜索最高总数,并检查日期是否已过。如果有有效的数据,则取前5个标签。

图片2在第二张图中,检查hashtag中的帖子是否在帖子中,如果有,则取最老的日期值,将LIMIT设置为1,并将来自循环表的id值与sid匹配。因此,可以访问共享人员的姓名。

图片3

我的英语有点差,我希望我能解释清楚。

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
并在subquery和social_trend
(假设两边都有)之间添加适当的连接

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

最新更新