我希望在页面顶部显示关键项目的总数和列表下方。这是我的查询
SELECT vs.vessel_name, vs.CallSign ,COUNT(IF(m.AvailableQuant <= m.MinQuant,1, NULL)) AS 'Critical'
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123
GROUP BY vs.id, vs.vessel_name ;
这是输出
vessel_name CallSign Critical_Items
----------- -------- ----------------
Sima Sadaf (NULL) 4
sdare 3q1 1
我想在页面顶部显示关键项目的总数,即5个项目是至关重要的 我怎么得到这笔钱?
答案 0 :(得分:0)
以下查询将返回关键项目的总和:
SELECT SUM(IF(m.AvailableQuant <= m.MinQuant,1, 0)) as total FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123;
答案 1 :(得分:0)
SELECT COUNT(IF(m.AvailableQuant <= m.MinQuant,1, NULL)) AS 'Critical'
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123
应该为您提供所有关键项目
答案 2 :(得分:0)
我希望您只想在现有查询中获得总数。
select sum(Critical) as Total from(
SELECT vs.vessel_name, vs.CallSign ,COUNT(IF(m.AvailableQuant <= m.MinQuant,1, NULL)) AS Critical
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123
GROUP BY vs.id, vs.vessel_name )alias;
答案 3 :(得分:0)
你尝试过这样的事吗?
select sum(d.Critical) from (SELECT vs.vessel_name, vs.CallSign ,COUNT(IF(m.AvailableQuant <= m.MinQuant,1, NULL)) AS 'Critical'
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123
GROUP BY vs.id, vs.vessel_name )d
答案 4 :(得分:0)
使用WITH ROLLUP可以获得额外的总计。然后确保先获取此值,并为呼号显示null:
SELECT
vessel_name,
case when vessel_name is not null then CallSign end as CallSign,
Critical
FROM
(
SELECT
vs.vessel_name,
vs.CallSign,
SUM(m.AvailableQuant <= m.MinQuant) AS Critical
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=123
GROUP BY vs.id WITH ROLLUP
) AS vessels
ORDER BY (vessel_name IS NOT NULL), vessel_name;