dongyata3336 2015-02-24 23:07
浏览 30
已采纳

MySQL - 只有在同一个表中没有获胜者设置但不同的行时才选择帖子ID。 包含示例代码

I have a wordpress table wp_postmeta and what I want is a list of all posts where sttf_split_test_status = 'active' but it CAN NOT have another meta row with the same post_id and a meta_key of stti_winner set. My below code returns empty, but there should be a result.

SELECT DISTINCT post_id 
FROM wp_postmeta 
WHERE meta_key = 'sttf_split_test_status' 
  AND meta_value = 'active' 
  AND NOT EXISTS (
    SELECT post_id 
    FROM wp_postmeta 
    WHERE post_id = post_id 
    AND meta_key = 'stti_winner'
  )
LIMIT 0, 25
  • 写回答

1条回答 默认 最新

  • douguxun6866 2015-02-24 23:16
    关注

    Try

    SELECT DISTINCT yes.post_id
    FROM wp_postmeta AS yes
    LEFT JOIN wp_postmeta AS no 
      ON yes.post_id=no.post_id
      AND no.meta_key='stti_winner'
    WHERE yes.meta_key = 'sttf_split_test_status' 
    AND yes.meta_value = 'active' 
    AND no.post_id IS NULL
    LIMIT 0, 25
    ;
    

    Explanation: To every fitting record we try to join the corresponding 'stti_winner'. Only if that fails (fields of LEFT JOIN stay NULL), we accept the record.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应