【问题标题】:Mysql Join - Get result in same rowMysql Join - 在同一行中获取结果
【发布时间】:2021-01-30 15:52:21
【问题描述】:
SELECT COUNT(status) AS COUNT
FROM financeinstallment
WHERE status = 1
GROUP BY financeId
UNION
SELECT finance.financeId,
       finance.customerId,
       customer.customerName,
       customer.phone1,
       customer.aadhaar,
       finance.financeStartDate,
       finance.vehicleNumber,
       finance.loanAmount,
       finance.totalInstallment,
       finance.InstallmentAmount
FROM finance
INNER JOIN customer ON customer.customerId = finance.customerId

如何在同一行得到结果?

我收到以下错误:

错误代码:1222。使用的 SELECT 语句具有不同的列数 0.000 秒

【问题讨论】:

  • 示例输入数据以及您想要查看的输出将极大地帮助您解决问题。
  • 我需要在单行中获取两个选择查询值

标签: mysql inner-join union


【解决方案1】:

这里的一种方法可能是将联合中的第二个查询作为子查询加入到第一个查询中。

SELECT
    t.count,
    f.financeId,
    f.customerId,
    c.customerName,
    c.phone1,
    c.aadhaar,
    f.financeStartDate,
    f.vehicleNumber,
    f.loanAmount,
    f.totalInstallment,
    f.InstallmentAmount
FROM finance f
INNER JOIN customer c
    ON c.customerId = f.customerId
INNER JOIN
(
    SELECT financeId, COUNT(status) AS count
    FROM financeinstallment
    WHERE status = 1
    GROUP BY financeId
) t
    ON f.financeId = t.financeId;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2019-04-30
    相关资源
    最近更新 更多