检查mySQL中另一个用户是否存在相同URL的方法

检查mySQL中另一个用户是否存在相同URL的方法,mysql,Mysql,首先,让我解释一下这里的逻辑。我们有一个由用户添加的文章组成的表。每个用户可以多次添加同一文章。然后我们就有了一个“墙”(类似于Facebook),上面显示了他们自己的文章和他们的任何“朋友”。以下是表格结构: CREATE TABLE IF NOT EXISTS `ReadingListArticles` ( `article_id` int(11) NOT NULL AUTO_INCREMENT, `user_id_fk` varchar(255) CHARACTER SET lat

首先,让我解释一下这里的逻辑。我们有一个由用户添加的文章组成的表。每个用户可以多次添加同一文章。然后我们就有了一个“墙”(类似于Facebook),上面显示了他们自己的文章和他们的任何“朋友”。以下是表格结构:

CREATE TABLE IF NOT EXISTS `ReadingListArticles` (
  `article_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id_fk` varchar(255) CHARACTER SET latin1 NOT NULL,
  `url` varchar(255) CHARACTER SET latin1 NOT NULL,
  `base_url` text CHARACTER SET latin1 NOT NULL,
  `message` text COLLATE utf8_bin NOT NULL,
  `timestamp` int(11) NOT NULL,
  `image_path` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `image_id` varchar(15) CHARACTER SET latin1 DEFAULT NULL,
  `title` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `media_source` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`article_id`),
  KEY `user_id_fk` (`user_id_fk`,`url`),
  KEY `image_id` (`image_id`),
  KEY `user_id_fk_2` (`user_id_fk`,`media_source`),
  KEY `media_source` (`media_source`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=0;
SQL非常简单:

SELECT * FROM ReadingListArticles WHERE 
user_id_fk IN ('their-username','friend1',,'friend2','etc')
AND base_url NOT IN ('list','of','sources','to','ignore') 
ORDER BY article_id DESC LIMIT 20
这很好,但有一个问题。。。如果他们和他们的朋友都分享了同一篇文章,它将在他们的列表中出现两次

我想知道(相当肯定这一定是可能的,但我只是不知道如何:)。。。。我们能否将其过滤掉,以便:

  • 如果文章由他们存在(给予优先权),则显示它
  • 如果他们不存在,我们可以从他们的任何朋友那里展示
希望我已经很好地解释了我自己,但是如果你需要更多的澄清,请让我知道

更新:我越来越近了:)

这是可行的,但它得到了错误的记录(它得到了最新的记录,只有当当前用户是最后一个发布准确URL的人时才是正确的,但实际上这不会发生)。在我的示例数据中,有两条记录具有相同的“url”:

然后,用户\u id\u fk会发生变化:

  • 测试123
  • 图表1
请求数据的用户是“test123”。。。因此,我们需要做的是在进行分组时优先考虑用户id值,而不仅仅是选择要与该URL一起添加的最新值

有人有什么新想法吗

谢谢

我现在使用(如建议的)以下查询:

SELECT ReadingListArticles.* from 
(
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk = 'Beata1234'
 and article_id > 170
 group by url
union
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk IN ('Beata1234')
 and article_id > 170
 and not exists (SELECT 1
   from ReadingListArticles rla1
   where rla1.user_id_fk = 'Beata1234'
   and rla1.article_id > 170
   and rla1.url = ReadingListArticles.url)
 group by url
) q
join ReadingListArticles
on ReadingListArticles.article_id = q.article_id
order by ReadingListArticles.article_id desc
limit 20;

但是,它在过滤方面存在问题。我有
article\u id>170
,但它仍然与
article\u id匹配,因为您希望以不同的方式对待主用户和其他用户,您可以将它们拆分。由于
article\u id
是表中唯一的唯一键,您可以使用

SELECT ReadingListArticles.* from 
(
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk = 'test123'
 and article_id > 0
 group by url
union
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk IN ('test2','designer1')
 and article_id > 0
 and not exists (SELECT 1
   from ReadingListArticles rla1
   where rla1.user_id_fk = 'test123'
   and rla1.article_id > 0
   and rla1.url = ReadingListArticles.url)
 group by url
) q
join ReadingListArticles
on ReadingListArticles.article_id = q.article_id
order by ReadingListArticles.article_id desc
limit 20;
您可能希望在
url,user\id\u fk
上添加索引,但根据您的数据,
user\id\u fk,url
也可以

这将为您提供:

  • 用户
    test123
    发布的所有文章的整行。如果
    test123
    多次使用某个url,则只会选择该url的最后一篇文章
  • 所有
    test2
    designer1
    已发布但url
    test123
    尚未发布的文章的整行。如果
    test2
    designer1
    发布了相同的url,或者如果他们多次发布了相同的url,则只选择其中的最后一篇文章

因为您想以不同的方式对待主用户和其他用户,所以您可以将它们分开。由于
article\u id
是表中唯一的唯一键,您可以使用

SELECT ReadingListArticles.* from 
(
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk = 'test123'
 and article_id > 0
 group by url
union
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk IN ('test2','designer1')
 and article_id > 0
 and not exists (SELECT 1
   from ReadingListArticles rla1
   where rla1.user_id_fk = 'test123'
   and rla1.article_id > 0
   and rla1.url = ReadingListArticles.url)
 group by url
) q
join ReadingListArticles
on ReadingListArticles.article_id = q.article_id
order by ReadingListArticles.article_id desc
limit 20;
您可能希望在
url,user\id\u fk
上添加索引,但根据您的数据,
user\id\u fk,url
也可以

这将为您提供:

  • 用户
    test123
    发布的所有文章的整行。如果
    test123
    多次使用某个url,则只会选择该url的最后一篇文章
  • 所有
    test2
    designer1
    已发布但url
    test123
    尚未发布的文章的整行。如果
    test2
    designer1
    发布了相同的url,或者如果他们多次发布了相同的url,则只选择其中的最后一篇文章

这可能会帮助您从阅读列表文章中选择*,如果(他们的-username.baseurl=friend1.baseurl,'high','low')作为优先顺序,其中用户id在('this-username','friend1','friend2','etc')中,而基本url不在('list','of','sources','to','ignore中),按文章顺序排列(id描述限制20
@AhmedKhan-感谢您的回复。我对此感到困惑:
如果(他们的-username.baseurl=friend1.baseurl,'high','low')
。。。那是什么意思?如果我使用这个:
If(test123.url=designer1.url,'high','low')
,我会得到一个错误:“字段列表”中的未知列'test123.url'。您只需要比较用户和他的朋友的url,然后得出优先级。我不知道你的桌子,所以我把它做成这样this@AhmedKhan-不幸的是,这没用。。这就是我问的原因;)我包括了上面的表格结构——这还不够吗?谢谢你试过数数吗<代码>选择*,如果(计数(基本url)>1,'high','low')作为优先阅读列表文章,其中用户id在('this-username','friend1','friend2','etc')和基本url不在('list','of','sources','to','ignore')中,按文章顺序\u-id描述限制20这是否有助于您
选择*,如果(他们的-username.baseurl=friend1.baseurl,'high','low'))作为阅读列表文章的优先权,其中用户id为“IN”(“他们的用户名”、“friend1”、“friend2”、“等”)和基本url不在(“列表”、“of”、“来源”、“to”、“忽略”)顺序按文章id描述限制20
@AhmedKhan-感谢您的回复。我对此感到困惑:
如果(他们的-username.baseurl=friend1.baseurl,'high','low')
。。。那是什么意思?如果我使用这个:
If(test123.url=designer1.url,'high','low')
,我会得到一个错误:“字段列表”中的未知列'test123.url'。您只需要比较用户和他的朋友的url,然后得出优先级。我不知道你的桌子,所以我把它做成这样this@AhmedKhan-不幸的是,这没用。。这就是我问的原因;)我包括了上面的表格结构——这还不够吗?谢谢你试过数数吗<代码>选择*,如果(计数(基本url)>1,'high','low')作为优先阅读列表文章,其中用户id在('this-username','friend1','friend2','etc')和基本url不在('list','of','sources','to','ignore')顺序按文章编号描述限制20您的图例!这就像一个符咒:)出于某种原因,它不会让我再给悬赏9个小时。我会尽力记住的
SELECT ReadingListArticles.* from 
(
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk = 'test123'
 and article_id > 0
 group by url
union
 select max(article_id) as article_id
 from ReadingListArticles 
 where user_id_fk IN ('test2','designer1')
 and article_id > 0
 and not exists (SELECT 1
   from ReadingListArticles rla1
   where rla1.user_id_fk = 'test123'
   and rla1.article_id > 0
   and rla1.url = ReadingListArticles.url)
 group by url
) q
join ReadingListArticles
on ReadingListArticles.article_id = q.article_id
order by ReadingListArticles.article_id desc
limit 20;