doujiyun8846 2014-04-27 23:54
浏览 15
已采纳

如何从匹配对的MySQL表中找到共享键值? [关闭]

I have a two column MySQL table which represents the variations of a product with three different options (size, colour etc.)

##############################
#  VariationID  #  OptionID  #
##############################
#            1  #        10  #
#            1  #        20  #
#            1  #        30  #
#            2  #        11  #
#            2  #        20  #
#            2  #        30  #
#            3  #        10  #
#            3  #        22  #
#            3  #        30  #
#            4  #        10  #
#            4  #        20  #
#            4  #        33  #
##############################

The table is generated by the PHP framework I'm using (Silverstripe) so I can't easily modify it.

How do I identify which VariationID is matched to specific options?

Can I run a MySQL query that returns the VariationID for the variation with the OptionIDs 10,20,30?

If it isn't possible in raw MySQL what's the best approach in PHP?

  • 写回答

1条回答 默认 最新

  • duangan6797 2014-04-28 00:01
    关注

    You can use SQL directly, but it is a bit messy:

    SELECT 
      A.VariationID AS VariationID
    FROM
      tablename AS A
      INNER JOIN tablename AS B ON A.VariationID=B.VariationID
      INNER JOIN tablename AS C ON A.VariationID=C.VariationID
    WHERE
      A.OptionID=10
      AND B.OptionID=20
      AND C.OptionID=30
    

    This will find the VariationID of rows, that exist for OptionID values of 10, 20 and 30

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据