【问题标题】:Laravel Mysql statement like fcm topic conditionLaravel Mysql 语句类似 fcm 主题条件
【发布时间】:2021-01-26 05:53:35
【问题描述】:

我正在尝试在mysql上制作类似于fcm的自己的主题系统。表就是这样:

user_id  topic_name

以 fcm 为例:

'topic1' in topics && 'topic2' in topics or ('topic3' in topics)

表示选择用户,订阅topic1和topic2或者订阅topic3。

在 mysql 中我不知道如何做到这一点,基本上它基于所有相等的 user_ids 但在 topic_name 上的不同条件但使用 whereIn 和 whereNotIn 之类的东西是不可能的,我真的很困惑。

【问题讨论】:

  • 在 mysql 中(主题 = 'topic1' AND 主题 = 'topic2')或主题 = 'topic3'
  • @nbk topic1 和 topic2 保存在 2 行中,这样就不行了。对吧?
  • 不,您可以将其更改为 OR 或使用主题 IN ( 'topic1','topic2' , 'topic3' )
  • 然后我该怎么办?
  • o 没有答案,因为我不知道您到底在寻找什么。做 quwry 看看你是否得到了所有需要的 rws

标签: mysql sql laravel eloquent having-clause


【解决方案1】:

我怀疑你想要聚合和 having 子句:

select user_id
from mytable
where topic_name in ('topic1', 'topic2', 'topic3')
group by user_id
having count(*) = 3

这会带来具有where 子句中列出的三个主题的用户。

【讨论】:

  • @stevemoretz:having 子句确保组中有 3 行,而 where 子句过滤您感兴趣的三个主题。结合在一起,确保用户拥有所有三个主题。
  • 谢谢你,但遗憾的是这并不能解决所有问题。它比这更复杂一点。基本上:\DB::select("select distinct user_id from push_notification_topics as r where ".preg_replace ( '/\'(.*?)\'.*?in topics/m', "user_id in (select user_id from push_notification_topics where r.user_id = user_id and topic_name = '$1')", preg_replace('/\| \|/m', 'or', preg_replace( '/&&/m', 'and', $condition ) ) ) ) 这是我得到的答案 我必须在第一个选择中选择表以使其检查这些东西也都是 and and or 作品。用一点正则表达式
猜你喜欢
  • 2020-01-18
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 2018-12-22
  • 2014-06-19
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多