Mysql - 连接查询没有公共列
Mysql - Join query no common column
我有这样的陈述:
SELECT
COUNT(*)AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
此查询是一个计数(*),因此结果只有一个字段。
但我想一次性执行此查询 6-7 次,仅更改此语句的(如“%will%”)部分,并并排获取每个计数的结果。
想要的结果:
我尝试左连接,像这样的全连接
(
SELECT
COUNT(*) AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
) AS will
LEFT JOIN
(
SELECT
COUNT(*) AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%phi%'
) AS phil
我不确定这是否可行。如果有人有解决办法。
解决方案
阅读您的一些评论后,我找到了另一种获取和显示数据的方法。
这是得到我的解决方案的语句。
SELECT
'Will' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
UNION
SELECT
'Phil' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%phi%'
UNION
SELECT
'Peter' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%Pet%'
我没有并排获得结果,而是一个在另一个之上。
如前所述,您的方案正试图根据条件进行调整。由于所有 3 个查询都是相同的日期范围,只需更改为 SUM( CASE/WHEN ),您将有 1 行包含所有 3 个计数。
SELECT
SUM( case when status_text LIKE '%wil%' then 1 else 0 end ) as CountWill,
SUM( case when status_text LIKE '%phi%' then 1 else 0 end ) as CountPhil,
SUM( case when status_text LIKE '%Pet%' then 1 else 0 end ) as CountPeter
from
ae_orders o
LEFT JOIN ae_orders_items oi
ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01'
AND po_date <= '2016-01-31'
我有这样的陈述:
SELECT
COUNT(*)AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
此查询是一个计数(*),因此结果只有一个字段。 但我想一次性执行此查询 6-7 次,仅更改此语句的(如“%will%”)部分,并并排获取每个计数的结果。
想要的结果:
我尝试左连接,像这样的全连接
(
SELECT
COUNT(*) AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
) AS will
LEFT JOIN
(
SELECT
COUNT(*) AS will
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%phi%'
) AS phil
我不确定这是否可行。如果有人有解决办法。
解决方案
阅读您的一些评论后,我找到了另一种获取和显示数据的方法。
这是得到我的解决方案的语句。
SELECT
'Will' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%wil%'
UNION
SELECT
'Phil' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%phi%'
UNION
SELECT
'Peter' AS USER,
COUNT(*) AS TOTAL
FROM
ae_orders o
LEFT JOIN
ae_orders_items oi ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01' AND po_date <= '2016-01-31' AND status_text LIKE '%Pet%'
我没有并排获得结果,而是一个在另一个之上。
如前所述,您的方案正试图根据条件进行调整。由于所有 3 个查询都是相同的日期范围,只需更改为 SUM( CASE/WHEN ),您将有 1 行包含所有 3 个计数。
SELECT
SUM( case when status_text LIKE '%wil%' then 1 else 0 end ) as CountWill,
SUM( case when status_text LIKE '%phi%' then 1 else 0 end ) as CountPhil,
SUM( case when status_text LIKE '%Pet%' then 1 else 0 end ) as CountPeter
from
ae_orders o
LEFT JOIN ae_orders_items oi
ON oi.order_uid = o.uid
WHERE
po_date >= '2016-01-01'
AND po_date <= '2016-01-31'