V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
Mac
V2EX  ›  MySQL

mysql 中 join + group by 的问题

  •  
  •   Mac · 2014-05-06 09:54:51 +08:00 · 4012 次点击
    这是一个创建于 3648 天前的主题,其中的信息可能已经有所发展或是发生改变。
    select *
    from
    bill
    left join container on bill.cid = container.cid
    left join main on container.ntno = main.ntno
    where main.ntno='140242'
    group by bill.shipper

    三个表,main表是索引,container表是main的子集以ntno连接,bill表是container表的子集以cid连接。
    用left join后输出一个以bill表为基础的补全container和main信息的数据集。数据是正确的。

    问题是,如果以bill表中的shipper字段做group by,问题就来了,这个shipper如果出现在不同container.cid中,group by合并就不起作用。何解?
    第 1 条附言  ·  2014-05-06 13:56:12 +08:00
    select main.ntno,container.c_no,bill.shipper,sum(bill.count_ctn),sum(bill.count_kgs),sum(bill.count_cbm)
    from
    bill
    left join container on bill.cid = container.cid
    left join main on container.ntno = main.ntno
    where main.ntno='140242'
    group by bill.shipper


    写全点吧,本来select *是想简化题目的,现在写全。目的是让所有bill.shipper相同的数据合并输出件数毛重体积。但实际效果,shipper如果在两个cid中,他就不合并。
    第 2 条附言  ·  2014-05-06 20:18:40 +08:00
    搞定了,原来是 /r 的问题。。。MYSQL真心强大,写这么乱的查询还能输出结果。第三方工具真心不靠谱,明明源数据是没有/r的,结果导出来个别记录加了/r,还看不出来。要不是单个复制出来粘帖一下还看不到,SHIT....
    6 条回复    2014-05-06 14:04:41 +08:00
    ybh37
        1
    ybh37  
       2014-05-06 11:11:36 +08:00
    你确定这样写没sql语法错误?
    lichao
        2
    lichao  
       2014-05-06 11:26:59 +08:00
    select 后面的内容,要么是聚合函数,要么是字段(字段必须出现在 group by 子句中),你 select * 是什么用意?
    qdvictory
        3
    qdvictory  
       2014-05-06 11:36:26 +08:00
    1. 写法问题,既然main是索引表,那么为什么不把main写在写面,这样反着写好蛋疼。
    2. container既然与bill是一对多的关系,那么你group by的时候就要在Select *中处理好显示的是哪一条,比如你的sql执行出来是3条,那么按你出错时出来的sql肯定是>3条的,因此你的group by将多条sql合并成了一条,而结果为先出现的那一条,即你的错误答案。解决方法同2楼。
    qdvictory
        4
    qdvictory  
       2014-05-06 11:38:50 +08:00
    @qdvictory 不能编辑好麻烦。。。少打了点东西
    你的sql执行出来是3条,那么按你出错时去掉group by出来的记录肯定是>3条的,因此你的group by将多条记录合并成了一条
    Mac
        5
    Mac  
    OP
       2014-05-06 13:58:37 +08:00
    @qdvictory

    select main.ntno,container.c_no,bill.shipper,sum(bill.count_ctn),sum(bill.count_kgs),sum(bill.count_cbm)
    from
    bill
    left join container on bill.cid = container.cid
    left join main on container.ntno = main.ntno
    where main.ntno='140242'
    group by bill.shipper


    写全点吧,本来select *是想简化题目的,现在写全。目的是让所有bill.shipper相同的数据合并输出件数毛重体积。但实际效果,shipper如果在两个cid中,他就不合并了,列出了两条记录。
    qdvictory
        6
    qdvictory  
       2014-05-06 14:04:41 +08:00
    @Mac 这样真的不好说看,建议你上全sql,从建表,插入测试数据,到sql。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1415 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:47 · PVG 00:47 · LAX 09:47 · JFK 12:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.