【问题标题】:Join Query to get data from multiple rows into a single rowJoin Query 将数据从多行获取到单行
【发布时间】:2018-05-31 15:39:16
【问题描述】:

从多行中选择数据到一行

Quote_ws table

quote_ws_id     quote_id    gross       tax
101                 79      98.25       13.0
102                 79      91.25       12.5    
103                 79      94.25       11.0
104                 79      92.25       11.5
105                 79      96.25       12.0


Section_table

section_id     quote_id    lsb(quote_ws_id)   non_lsb(quote_ws_id)
1               79          101                     null
2               79          102                     103
3               79          104                     105

我正在尝试创建一个 MYSQL 查询,该查询将根据“Section”表中的 lsb 和非 lsb 值从表“Quote_ws”中返回总值和税值。结果将是:

section_id     quote_id    gross                tax
1               79          98.25               13.0


2               79          91.25 + 94.25       12.5+11.0
                        (lsb 102,non-lsb 103)   (lsb 102,non-lsb 103)

3               79          92.25 + 96.25       11.5+12.0       
                        (lsb 104,non-lsb 105)   (lsb 104,non-lsb 105)

我是 SQL 新手。我怎样才能做到这一点?请帮帮我??

【问题讨论】:

  • 你试过什么?向我们展示你的尝试。
  • 请以文本形式提供数据

标签: php mysql sql


【解决方案1】:
SELECT
s.section_id,
s.quote_id,
(IFNULL(q_lbs.gross, 0)+IFNULL(q_non_lbs.gross, 0)) as gross,
(IFNULL(q_lbs.tax, 0)+IFNULL(q_non_lbs.tax, 0)) as tax,
FROM
Section_table s

LEFT JOIN Quote_ws q_lbs
ON q.quote_id = q.quote_id

LEFT JOIN Quote_ws q_non_lbs
ON q.quote_id = q.quote_id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多