我正在尝试为项目组获取大量图像,但似乎无法使用指定的别名进行乘法运算。我已经尝试了下面的脚本并进行了一些修改,但我尝试过的每件事都无法使用。
SELECT s.pid AS 'ID',
CONCAT_WS(', ', p.lastname, p.firstname) AS 'Name',
CONCAT(s.date, ' ', s.time) AS Serviced,
p.institution AS Facility,
s.description AS 'description title',
(SELECT COUNT(*)
FROM series se
WHERE se.studyid = s.id)
AS numseries,
(SELECT COUNT(*)
FROM image i
WHERE i.seriesid = se.id)
AS serimages,
numseries * serimages AS 'Number of Images'
FROM product p,
series se
JOIN
study s
ON s.pid = p.id
GROUP BY s.id
ORDER BY Serviced DESC
答案 0 :(得分:3)
试试这个,我同时挫败了 -
SELECT s.pid AS 'ID',
CONCAT_WS(', ', p.lastname, p.firstname) AS 'Name',
CONCAT(s.date, ' ', s.time) AS Serviced,
p.institution AS Facility,
s.description AS 'description title',
((SELECT COUNT(*)
FROM series se
WHERE se.studyid = s.id)
*
(SELECT COUNT(*)
FROM image i
WHERE i.seriesid = se.id)
),
AS 'Number of Images'
FROM product p,
series se
JOIN
study s
ON s.pid = p.id
GROUP BY s.id
ORDER BY Serviced DESC
答案 1 :(得分:1)
尝试使用它
SELECT
t.ID,
t.Name,
t.Serviced,
t.Facility,
t.title,
t.numseries,
t.serimages,
t.numseries * t.serimages AS 'Number of Images'
FROM
(
SELECT s.pid AS 'ID',
CONCAT_WS(', ', p.lastname, p.firstname) AS 'Name',
CONCAT(s.date, ' ', s.time) AS Serviced,
p.institution AS Facility,
s.description AS 'description title',
(SELECT COUNT(*) FROM series se WHERE se.studyid = s.id) AS numseries,
(SELECT COUNT(*) FROM image i WHERE i.seriesid = se.id) AS serimages
FROM product p, series se
JOIN study s ON s.pid = p.id
) as t
GROUP BY t.id
ORDER BY t.Serviced DESC
此外,您在查询中使用笛卡尔积,我的意思是使用FROM和两个表。这带来了很多结果。避免它,并始终使用连接。
答案 2 :(得分:0)
试试这个:
Select *, ( numseries * serimages ) as 'Number of Images'
from (SELECT
s.pid AS 'ID',
CONCAT_WS(', ', p.lastname, p.firstname) AS 'Name',
CONCAT(s.date, ' ', s.time) AS Serviced,
p.institution AS Facility,
s.description AS 'description title',
(SELECT COUNT(*) FROM series se WHERE se.studyid = s.id) AS numseries,
(SELECT COUNT(*) FROM image i WHERE i.seriesid = se.id) AS serimages
FROM product p, series se
INNER JOIN study s ON s.pid = p.id
GROUP BY s.id
ORDER BY Serviced DESC
) as a;