【问题标题】:sql join two tables, one summary the other is detailsql连接两张表,一张是summary,一张是detail
【发布时间】:2016-05-25 06:47:49
【问题描述】:

当连接两个表时,我不确定如何将以下表连接到我想要的确切位置。

表 A:

-------------------------------------- |编号 |姓名 |购买时间 |总计| -------------------------------------- | 1 |一个 | 3 | 30 | -------------------------------------- | 2 |乙| 1 | 10 | --------------------------------------

表 B:

------------------------------------------- |编号 |订单号 |价格 | ------------------------------------------- | 1 | 1 | 10 | ------------------------------------------- | 1 | 2 | 10 | ------------------------------------------- | 1 | 3 | 10 | ------------------------------------------- | 2 | 4 | 10 | -------------------------------------------

加入表 C

-------------------------------------------------- -------- |编号 |姓名 |购买时间 |总计|订单号 |价格 | -------------------------------------------------- -------- | 1 |一个 | 3 | 30 | | | -------------------------------------------------- -------- | 1 | | | | 1 | 10 | -------------------------------------------------- -------- | 1 | | | | 2 | 10 | -------------------------------------------------- -------- | 1 | | | | 3 | 10 | -------------------------------------------------- -------- | 2 |乙| 1 | 10 | | | -------------------------------------------------- -------- | 2 | | | | 4 | 10 | -------------------------------------------------- --------

如果我在 A.id = B.id 上使用“Left OUT JOIN” 空白区域会被重复值填满,导致总收入不正确

另一种方法是“从B中选择0作为名称”,用0填充空白,如果列数过多,这将是一场灾难。

因此,我想寻求帮助,有没有更好的方法来实现我的目标?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您真正想要的是两个表的UNION

    SELECT id, name, `buy time`, total, null AS orderid, null AS price
    FROM A
    
    UNION 
    
    SELECT id, null, null, null, orderid, price
    FROM B
    ORDER BY id, name DESC, orderid
    

    Demo here

    【讨论】:

      【解决方案2】:

      看来您需要UNION ALL

      SELECT `id`,`name`,`buy time`,`total`,null,null
      FROM TableA 
      UNION ALL
      SELECT id,null,null,null,orderid,price
      FROM TableB 
      ORDER BY `id`,`name` ,orderid
      

      无需进行这种不必要的连接,因为您不会以任何方式将它们相互连接。

      【讨论】:

        【解决方案3】:

        看起来你需要使用UNION

        select id, name, `buy time`, total, null AS orderid, null AS price
        from A
        UNION 
        select id, null, null, null, orderid, price
        from B
        order by id, name DESC
        

        【讨论】:

          【解决方案4】:

          选择 ID、名称、购买时间、总计、订单号、价格来自 (

            SELECT  [id]
           , null as [orderid]
            ,null as [price],
             [name]
            ,[buytime]
            ,[total]
             FROM A
               UNION
          
           SELECT [id]
            ,[orderid]
            ,[price],
            null as[name]
            , null as[buytime]
            ,null as [total]
           FROM B
          

          )as aa

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-20
            • 2017-04-26
            • 1970-01-01
            • 1970-01-01
            • 2023-03-31
            • 2011-04-30
            • 2016-08-29
            • 2017-07-17
            相关资源
            最近更新 更多