MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
longnight0119
V2EX  ›  MySQL

mysql 复合主键 group by 查询之后 如何 having count

  •  
  •   longnight0119 · Feb 13, 2018 · 4620 views
    This topic created in 3037 days ago, the information mentioned may be changed or developed.

    mysql 表 table PK(id,type) 关联表 table1 (多列+table.id+table.type) 在 table1 中 执行查询 select * from table1 group by id,type having count(querySql)=2 请问这个 querySql 该怎么写

    6 replies    2018-02-14 10:48:33 +08:00
    longnight0119
        1
    longnight0119  
    OP
       Feb 13, 2018
    我目前用的是 having count(concat(id,type))=2 这样的方式 有没有更好的方式
    akira
        2
    akira  
       Feb 14, 2018
    select id, `type`, ... , count(1) `c` from table1
    where ...
    group by id, `type`
    having `c` =2
    usedname
        3
    usedname  
       Feb 14, 2018 via iPhone
    ID type 做复合主键也是清奇
    longnight0119
        4
    longnight0119  
    OP
       Feb 14, 2018
    @akira 嗯 这个也不错 我之前在 子查询中 用到了 新增列 在标识 数据来自哪个表 我倒是忘了 可以复用这种方式
    longnight0119
        5
    longnight0119  
    OP
       Feb 14, 2018
    @usedname 多系统的应用 没有办法中的办法
    longnight0119
        6
    longnight0119  
    OP
       Feb 14, 2018
    @akira 不行 count ( 1 ) c 后 c 的列值都是 1 两个表 union all 后 再用 count()聚合函数 只有两条数据
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1288 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 17:17 · PVG 01:17 · LAX 10:17 · JFK 13:17
    ♥ Do have faith in what you're doing.