donglengli0644 2015-04-06 17:52
浏览 140
已采纳

如何在同一个表中对多个列值进行数据匹配[重复]

This question already has an answer here:

How can I get the ID of those books which belong to country 79 AND author 80 AND publisher 91

SELECT id FROM books WHERE ( 
    (filter = 'country' AND cat = 79) 
        AND
    (filter = 'author' AND cat = 80)
        AND
    (filter = 'publisher' AND cat = 91)
    )   

Here is my table structure

+----+----------+-----+
| ID | FITER    | CAT |
+----+----------+-----+
|  1 | country  | 79  |
|  1 | author   | 80  |
|  1 | publisher| 91  |
|  2 | country  | 476 |
|  2 | author   | 85  |
|  2 | publisher| 121 |
+----+----------+-----+         

Thank you

</div>
  • 写回答

1条回答 默认 最新

  • duanfang7757 2015-04-06 17:56
    关注

    This is the problem with this kind of table structure, do you really have that many filters that they can't be columns on their own?. Anyway, it would be like this:

    SELECT DISTINCT id 
    FROM books b
    WHERE filter = 'country' AND cat = 79
    AND EXISTS( SELECT 1 FROM books 
                WHERE filter = 'author' AND cat = 80
                AND id = b.id)
    AND EXISTS( SELECT 1 FROM books 
                WHERE filter = 'publisher' AND cat = 91
                AND id = b.id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里