如何才能明确地计算我从联合查询中收到的值?

时间:2015-11-12 05:39:34

标签: mysql sql

我有这个问题:

SELECT Pedido1 from mydb.atendimentos 
UNION ALL
SELECT Pedido2 from mydb.atendimentos
order by Pedido1 ASC

这让我得到了这个结果: What I get when executing the query 现在我要提供的是:

Teste -> 3
Teste2 -> 1

有没有办法用联盟做到这一点?

1 个答案:

答案 0 :(得分:2)

这称为派生表。

http://www.programmerinterview.com/index.php/database-sql/derived-table-vs-subquery/

SELECT Pedido1, COUNT(*) AS PedidoCount
FROM 
(
    SELECT Pedido1 FROM mydb.atendimentos 
    UNION ALL
    SELECT Pedido2 FROM mydb.atendimentos
) T
GROUP BY Pedido1
ORDER BY Pedido1