Add mysql columns

Asked

Viewed 71 times

0

Good afternoon, I opened a question yesterday but I could not express myself correctly, I have the following query

SELECT C.id, C.name as NomePC, C.serial AS Serial, MF.name AS Fabricante, CM.name AS ModeloPC,OS.name AS Sistema, OSV.name AS OSVersao,DP.designation AS Processador, IDH.capacity AS CapacidadeHD, DH.designation AS ModeloHD, DGC.designation AS PlacaVideo, IDM.size AS Memoria, DM.designation AS tipoMemoria FROM glpi_computers AS C INNER JOIN glpi_manufacturers AS MF ON C.manufacturers_id = MF.id INNER JOIN glpi_computermodels as CM ON C.computermodels_id = CM.id INNER JOIN glpi_operatingsystems AS OS ON C.operatingsystems_id = OS.id INNER JOIN glpi_operatingsystemversions AS OSV ON C.operatingsystemversions_id = OSV.id INNER JOIN glpi_items_deviceprocessors AS IDP ON C.id = IDP.items_id INNER JOIN glpi_deviceprocessors AS DP on IDP.deviceprocessors_id = DP.id INNER JOIN glpi_items_deviceharddrives AS IDH on C.id = IDH.items_id INNER JOIN glpi_deviceharddrives AS DH on IDH.deviceharddrives_id = DH.id INNER JOIN glpi_items_devicegraphiccards AS IDGC on C.id = IDGC.items_id INNER JOIN glpi_devicegraphiccards AS DGC ON IDGC.devicegraphiccards_id = DGC.id INNER JOIN glpi_items_devicememories AS IDM on C.id = IDM.items_id INNER JOIN glpi_devicememories AS DM on IDM.devicememories_id = DM.id WHERE C.id = 271 AND IDH.capacity != 0

and I’m getting the following result

query atual

when the PC has more than one ram memory comb I get 2 query results as shown in the image, I need to get only ONE result however ADDING the amount of memory as in the image below (I had to erase the image for not having enough reputation to post 3 images)

how can I do this by mysql itself?

RESOLVED

Now I have another problem, I tried to replicate the solution (use SUM) in HD, however it ta doubling the size of my hard drive, it is only 500gb and as a result of the query I am getting double hd

1 answer

2


Just use the function SUM(coluna) in the memory column, and group by the others which shall be equal, by Group By

Follows code:

SELECT 
C.id, 
C.name as NomePC, 
C.serial AS Serial, 
MF.name AS Fabricante,
 CM.name AS ModeloPC,
OS.name AS Sistema, 
OSV.name AS OSVersao,
DP.designation AS Processador, 
IDH.capacity AS CapacidadeHD, 
DH.designation AS ModeloHD, 
DGC.designation AS PlacaVideo, 
SUM(IDM.size) AS Memoria, 
DM.designation AS tipoMemoria
    FROM glpi_computers AS C
    INNER JOIN glpi_manufacturers AS MF ON C.manufacturers_id = MF.id
    INNER JOIN glpi_computermodels as CM ON C.computermodels_id = CM.id
    INNER JOIN glpi_operatingsystems AS OS ON C.operatingsystems_id = OS.id
    INNER JOIN glpi_operatingsystemversions AS OSV ON C.operatingsystemversions_id = OSV.id
    INNER JOIN glpi_items_deviceprocessors AS IDP ON C.id = IDP.items_id
    INNER JOIN glpi_deviceprocessors AS DP on IDP.deviceprocessors_id = DP.id 
    INNER JOIN glpi_items_deviceharddrives AS IDH on C.id = IDH.items_id
    INNER JOIN glpi_deviceharddrives AS DH on IDH.deviceharddrives_id = DH.id
    INNER JOIN glpi_items_devicegraphiccards AS IDGC on C.id = IDGC.items_id 
    INNER JOIN glpi_devicegraphiccards AS DGC ON IDGC.devicegraphiccards_id = DGC.id
    INNER JOIN glpi_items_devicememories AS IDM on C.id = IDM.items_id
    INNER JOIN glpi_devicememories AS DM on IDM.devicememories_id = DM.id
    WHERE C.id = 271 AND IDH.capacity != 0

    GROUP BY C.id, C.name, C.serial ,  MF.name, CM.name ,  OS.name , 
OSV.name ,  DP.designation ,   IDH.capacity ,  DH.designation , 
DGC.designation ,  DM.designation

Edit:

To add, hard disk size, and amount of memory, without grouping by other columns:

SELECT 
C.id, 
C.name as NomePC, 
C.serial AS Serial, 
MF.name AS Fabricante,
 CM.name AS ModeloPC,
OS.name AS Sistema, 
OSV.name AS OSVersao,
DP.designation AS Processador, 
(Select Sum(IDH.capacity) from glpi_items_deviceharddrives AS IDH where IDH.items_id = C.id) AS CapacidadeHD, 
DGC.designation AS PlacaVideo, 
(Select  SUM(IDM.size) from glpi_items_devicememories AS IDM where IDM.items_id = C.id) AS Memoria
    FROM glpi_computers AS C
    INNER JOIN glpi_manufacturers AS MF ON C.manufacturers_id = MF.id
    INNER JOIN glpi_computermodels as CM ON C.computermodels_id = CM.id
    INNER JOIN glpi_operatingsystems AS OS ON C.operatingsystems_id = OS.id
    INNER JOIN glpi_operatingsystemversions AS OSV ON C.operatingsystemversions_id = OSV.id
    INNER JOIN glpi_items_deviceprocessors AS IDP ON C.id = IDP.items_id
    INNER JOIN glpi_deviceprocessors AS DP on IDP.deviceprocessors_id = DP.id 
    INNER JOIN glpi_items_devicegraphiccards AS IDGC on C.id = IDGC.items_id 
    INNER JOIN glpi_devicegraphiccards AS DGC ON IDGC.devicegraphiccards_id = DGC.id
    WHERE C.id = 271 and (Select Sum(IDH.capacity) from glpi_items_deviceharddrives AS IDH where IDH.items_id = C.id) > 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.