按列组合mysql查询
是否存在按列组合mysql查询的方法?我有以下疑问按列组合mysql查询,mysql,sql,Mysql,Sql,是否存在按列组合mysql查询的方法?我有以下疑问 select count(ticket_id) as numInProgress from ost_ticket where status = 'in progress' and topic_id not in (select topic_id from ost_org_site_map) select count(ticket_id) as numAssigned from ost_ticket where status = 'o
select count(ticket_id) as numInProgress from ost_ticket where status
= 'in progress' and topic_id not in (select topic_id from ost_org_site_map)
select count(ticket_id) as numAssigned from ost_ticket where status =
'open' and topic_id not in (select topic_id from ost_org_site_map)
我正试图得到以下格式的结果
---numInProgress | numAssigned---
2 | 8
---------------------------------
只需将它们用作select语句中的子查询,例如
SELECT ($statement1$),($statement2$)
或与您的查询:
SELECT (select count(ticket_id) as numInProgress from ost_ticket where status = 'in progress' and topic_id not in (select topic_id from ost_org_site_map)) as numInProgress ,(select count(ticket_id) as numAssigned from ost_ticket where status = 'open' and topic_id not in (select topic_id from ost_org_site_map)) as numAssigned;
你可以试试这个:
SELECT (select count(ticket_id) as numInProgress
from ost_ticket where status = 'in progress'
and topic_id not in (select topic_id from ost_org_site_map)),
(select count(ticket_id) as numAssigned
from ost_ticket where status = 'open'
and topic_id not in (select topic_id from ost_org_site_map));
ie将两个查询合并在select语句中生成一个子查询。可能是这样的:
SELECT sum(CASE OT.status when 'in progress' ,1,0) as numInProgress,
sum(CASE OT.status when 'open' ,1,0) as numAssigned
FROM ost_ticket OT
LEFT JOIN OST_ORG_SITE_MAP OSM
ON OT.topic_Id =OSM.Topic_ID
WHERE OT.status in ('in progress', 'open')
and OSM.Topic_ID is null
左边的连接允许我们在主题Id上连接OST_票证和OST_组织站点地图。当站点地图表中没有主题Id时,我们有一个等价于主题Id不在
我们将状态合并为只查找进行中和打开的状态,并使用状态的案例语句来确定需要计算的内容。由于count将返回1和0的值,因此我们需要使用sum将1和0相加