首页 > 解决方案 > 使用 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

标签: mysqlinner-joinsql-like

解决方案


您应该使用 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

推荐阅读