gpt4 book ai didi

mysql - 如何从基于单列的 SQL 查询中删除重复项

转载 作者:行者123 更新时间:2023-11-29 05:29:43 25 4
gpt4 key购买 nike

有一个查询需要修改。查询当前所做的是根据 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')";

最佳答案

执行此操作的“正确”方法是首先找出重复出现的原因,从而解决路由原因。这将与 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 上重新连接,并重新连接到广告和其他表(给它们不同的别名),以便从您的原始查询中选择字段。

关于mysql - 如何从基于单列的 SQL 查询中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16049171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com