› 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
cheeseleng
V2EX  ›  MySQL

mysql 表数据根据某个相同字段合并的的 sql 语句怎么写?两张表结构不一样,有一个相同字段

  •  
  •   cheeseleng · Aug 31, 2017 · 4925 views
    This topic created in 3320 days ago, the information mentioned may be changed or developed.

    如题,举例

    希望能根据 merchant_name 这个字段将两张表联合起来 就是下面这样

    | YEAR        | MONTH  | COUNT | sum | merchant_name | MIN(date) |
    | ------------|:------:| -----:| ---:| -------------:|-----------|
    | 2017        | 7      |    1  | 100 |   广东        | 2017-07-25|
    | 2017        | 7      |    1  | 100 |   苏果        | 2017-07-19|
    | 2017        | 8      |    1  | 100 |   苏果        | 2017-07-19|
    

    表一的 sql

    SELECT
    	YEAR (ioi.create_date),
    	MONTH (ioi.create_date),
    	count(*),
    	sum(ioi.order_amount),
    	imi.merchant_name
    FROM
    	inst_order_info AS ioi
    LEFT JOIN inst_merchant_info imi ON imi.store_id = ioi.store_id
    GROUP BY
    	date_format(ioi.create_date, '%Y-%m'),
    	imi.merchant_name;
    

    表二的 sql

    SELECT
    	MIN(ioi.create_date),imi.merchant_name
    FROM
    	inst_order_info ioi
    LEFT JOIN inst_merchant_info imi ON imi.store_id = ioi.store_id
    GROUP BY
    	imi.merchant_name
    
    1 replies  •  2017-08-31 14:06:44 +08:00
    cheeseleng
        1
    cheeseleng  
    OP
       Aug 31, 2017
    已经解决了,就用 LEFT JOIN 连接就可以了,思路不正确走了弯路:(
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   772 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:14 · PVG 04:14 · LAX 13:14 · JFK 16:14
    ♥ Do have faith in what you're doing.