简体   繁体   English

MySQL - 按别名乘以列

[英]MySQL - Multiply columns by alias

I am trying to get a number of images for an item group but cant seem to get the multiplication to work with the alias assigned. 我试图为一个项目组获取一些图像,但似乎无法使用指定的别名进行乘法运算。 I have tried the script below with some modifications but every thing I have tried will not work. 我已经尝试了下面的脚本进行了一些修改,但我尝试过的每件事都行不通。

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

Try this, i clobbed both- 试试这个,我挫败了两个 -

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

Try using it alike this 尝试使用它

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

Also you are usig cartisin product in your query i mean using FROM with two tables. 您在查询中也是usig cartisin产品,我的意思是使用FROM和两个表。 This brings a lot of results. 这带来了很多结果。 Avoid it and always use join instead. 避免它,并始终使用连接。

Try this: 尝试这个:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM