码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 分组后成员作为集合两两运算


    【问题】

    Suppose I have a MySQL database in 1NF with the following tables:

    1. sandwich\_id | sandwich\_name | sandwich_price
    2. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
    3. 0| BLT | 5.5
    4. 1| Reuben | 7.0
    5. 3| Grilled Cheese | 3.75
    6. ...

    and a separate table that stores all the ingredient values:

    1. sandwich_id | ingredient
    2. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
    3. 0| bacon
    4. 0| lettuce
    5. 0| tomato
    6. 1| corned beef
    7. 1| swiss cheese
    8. ...

    How can I compare all the sandwiches by their ingredients to determine which are the most similar?

    (Also, is there a technical term for that second table that I’m missing? I want to call it a map table, but I know that’s not quite right, since a map table stores foreign keys for two tables and this one’s more of an offshoot of the first…)

    【回答】

    把不同 sandwich 的 ingredient 集合两两做交集,计算交集成员数量再排序即可。但 SQL 没有显式集合,代码很难懂。这种数据量不大时用 SPL 更方便:

    A
    1$select i.sandwich_id sandwich_id, i.ingredient ingredient, s.sandwich_name sandwich_namefrom ingredientTable i,sandwichTable s where i.sandwich_id=s.sandwich_id
    2=A1.group(sandwich_id;sandwich_name,~.(ingredient):collection)
    3=xjoin(A2;A2).select(_1.sandwich_id<_2.sandwich_id)
    4=A3.new((_1.collection ^ _2.collection).len():sameCount,_1.sandwich_name,_2.sandwich_name)
    5=A4.sort(-sameCount)

    A2: 将 A1 按 sandwich_id 分组。collection 是每种 sandiwich 的 ingredient 集合。

    A3: 将 sandwich 两两分为一组。比如 0,1,3 可分为三组:[0,1],[0,3],[1,3]

    A4: 求各组交集的成员数量。“^”表示求交集。

    A5: 将 A4 的结果集降序排序

    关于显式集合请参考【集算器的集合思维】。更多例子可参考【集算器的集合运算举例】

     

  • 相关阅读:
    【CV论文阅读】【计算机视觉中的Transformer应用综述】(1)
    AgileConfig-1.9.4 发布,支持 OpenTelemetry
    【Go】命令行相关
    一线互联网大厂普遍使用的Docker,掌握这套面试题,让领导主动涨薪
    Java:本地文件通过表单参数接口发送后大小变成0
    最全面的创建线程的几种方式总结,让你的需求轻松选择最合适你的线程创建方式
    【靶场搭建】-01- 在kali上搭建DVWA靶机
    Django--inclusion_tag
    Shell编程三剑客之sed
    什么是计算机网络
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/127645700
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号