1

我需要修改一个查询。查询当前所做的是根据 Ad Title 和 Ad Description 返回搜索结果(广告)。如果在广告标题或广告描述中找到任何搜索词,则会返回这些结果

我想修改查询,以便每个广告在给定广告标题的搜索结果中只出现一次...因此,如果在搜索中找到 5 个广告与给定单词的广告标题相同,它应该只返回 1 个广告对于那个广告标题...

$sql = "SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, ct.cityname,
                    COUNT(*) AS piccount, p.picfile,
                    scat.subcatname, cat.catid, cat.catname $xfieldsql
                FROM t_ads a
                    INNER JOIN t_cities ct ON a.cityid = ct.cityid
                    INNER JOIN t_subcats scat ON a.subcatid = scat.subcatid
                    INNER JOIN t_cats cat ON scat.catid = cat.catid
                    LEFT OUTER JOIN t_adxfields axf ON a.adid = axf.adid
                    LEFT OUTER JOIN t_adpics p ON a.adid = p.adid AND p.isevent = '0'
                    LEFT OUTER JOIN t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A'
                WHERE $where
                    AND $visibility_condn
                    AND (feat.adid IS NULL OR feat.featuredtill < NOW())
                    $loc_condn
                GROUP BY a.adid
                ORDER BY a.createdon DESC
                LIMIT $offset, $ads_per_page";

编辑: $where 包含搜索表达式...如果打开正则表达式搜索,则使用正则表达式,否则不... $sqlsearch 包含用户输入的搜索词...

if ($regex_search) {
                $where = "(a.adtitle RLIKE '[[:<:]]{$searchsql}[[:>:]]' OR a.addesc RLIKE '[[:<:]]{$searchsql}[[:>:]]')";
            } else {
                $where = "(a.adtitle LIKE '$searchsql' OR a.addesc LIKE '$searchsql')";
4

1 回答 1

3

执行此操作的“正确”方法是通过首先找出重复出现的原因来解决路由原因。这将与 JOIN 有关,但如果不查看数据,我无法回答这个问题。但是,如果您想要一种快速(ish)且肮脏的方法来删除重复项,则可以尝试以下方法。

免责声明:这完全未经测试,因此这里更有可能出现一两个错误 - 但希望不会破坏交易。

SELECT a2.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, ct2.cityname,
       COUNT(*) AS piccount, p2.picfile,
       scat2.subcatname, cat2.catid, cat2.catname $xfieldsql
FROM
   (SELECT subq1.title, MIN(subq1.adid) AS adid
    FROM 
           (SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, ct.cityname,
                COUNT(*) AS piccount, p.picfile,
                scat.subcatname, cat.catid, cat.catname
            FROM t_ads a
                INNER JOIN t_cities ct ON a.cityid = ct.cityid
                INNER JOIN t_subcats scat ON a.subcatid = scat.subcatid
                INNER JOIN t_cats cat ON scat.catid = cat.catid
                LEFT OUTER JOIN t_adxfields axf ON a.adid = axf.adid
                LEFT OUTER JOIN t_adpics p ON a.adid = p.adid AND p.isevent = '0'
                LEFT OUTER JOIN t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A'
            WHERE $where
                AND $visibility_condn
                AND (feat.adid IS NULL OR feat.featuredtill < NOW())
                $loc_condn
            GROUP BY a.adid) subq1
    GROUP BY subq.title) subq2
INNER JOIN t_ads a2 ON a2.adid = subq2.adid
INNER JOIN t_cities ct2 ON a2.cityid = ct2.cityid
INNER JOIN t_subcats scat2 ON a2.subcatid = scat2.subcatid
INNER JOIN t_cats cat2 ON scat2.catid = cat2.catid
LEFT OUTER JOIN t_adxfields axf2 ON a2.adid = axf2.adid
LEFT OUTER JOIN t_adpics p2 ON a2.adid = p2.adid AND p2.isevent = '0'
LEFT OUTER JOIN t_featured feat2 ON a2.adid = feat2.adid AND feat2.adtype = 'A'
ORDER BY a2.createdon DESC
LIMIT $offset, $ads_per_page

这可以大大简化和整理,例如通过从子查询中删除一些东西,但我只是给出一个总体思路(希望)让你启动并运行......

解释

subq2只需按标题分组并adid从每个组中挑选一个(选择在MIN此处使用,但可以MAX改为使用)。

subq1是原始查询,但删除了排序和限制,因为这些是由外部查询应用的。

外部查询连接到重复数据删除的 ID 并连接回广告和其他表(给它们不同的别名),以便从原始查询中选择字段。

于 2013-04-17T19:52:26.270 回答