doubi7306 2012-12-18 15:37
浏览 26
已采纳

如何从表中选择记录,其中记录具有来自另一个表的最相似属性

TABLE A
result   select
A              a
B              a
B              b
C              a
C              b
C             c

TABLE B

user   select

1              a
1              b
2              b
2              c

TABLE C
user   result
1         B
2         C


what mysql query should i have to use, if i want to do like this:

if user select a the result is A B C (because A B C have 'a' in column 'select') if user select a b the result is B C (because A doesn't have 'b' in column 'select') if user select a b c the result is C (because C have 'a b c' in column 'select') if user select b c the result is C (because A B doesn't have 'c' in column 'select') if user select c the result is C (because A B doesn't have 'c' in column 'select')

so it will select the record that have the most similar from what user selected and ignore the other record that doesn't match.. sorry for my english, i'm speak bahasa...

  • 写回答

1条回答 默认 最新

  • douchong8393 2012-12-18 15:39
    关注
    SELECT  `result`
    FROM tableA
    WHERE   `select` IN ('a','b','c')
    GROUP BY result
    HAVING COUNT(*) = 3
    

    if a unique constraint was not enforce on select for every result, a DISTINCT is required.

    SELECT  `result`
    FROM tableA
    WHERE   `select` IN ('a','b','c')
    GROUP BY result
    HAVING COUNT(DISTINCT `select`) = 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染