简体   繁体   中英

Proper mysql code to alter table to horizontal

I am trying to make the table below look like so

Current Query:

SELECT m.typeID, t.typeName, m.quantity, t.description
FROM invTypeMaterials AS m
LEFT JOIN invTypes AS t
ON m.materialTypeID = t.typeID
LEFT JOIN invTypes
ON m.TypeID = t.typeID
WHERE m.typeID = 10039

Current Result

typeID  typeName    quantity    description     
10039   Tritanium   71  The main building block in space structures. A ver...
10039   Pyerite     24  A soft crystal-like mineral with a very distinguis...
10039   Mexallon    1   Very flexible metallic mineral, dull to bright sil...

What I'm look for is for it to show up like this

 typeID     Tritanium   Pyerite  mexallon   description     
10039       71            24        1       Doesn't matter 

I have tried combinations of left join, outer join, inner join etc and cant make it work. I also tried group_concat which I think is what I need but cant get the columns right

Thanks

EDIT: I have the column looking like I want by using the following:

SELECT m1.typeID,
SUM(CASE WHEN m1.materialTypeID = 34 THEN m1.quantity ELSE 0 END) AS Tritanium,
    SUM(CASE WHEN m1.materialTypeID = 35 THEN m1.quantity ELSE 0 END) AS Pyerite,
    SUM(CASE WHEN m1.materialTypeID = 36 THEN m1.quantity ELSE 0 END) AS Mexallon,
    SUM(CASE WHEN m1.materialTypeID = 37 THEN m1.quantity ELSE 0 END) AS Isogen,
    SUM(CASE WHEN m1.materialTypeID = 38 THEN m1.quantity ELSE 0 END) AS Nocxium,
    SUM(CASE WHEN m1.materialTypeID = 39 THEN m1.quantity ELSE 0 END) AS Zydrine,
    SUM(CASE WHEN m1.materialTypeID = 40 THEN m1.quantity ELSE 0 END) AS Megacyte,
    SUM(CASE WHEN m1.materialTypeID = 11399 THEN m1.quantity ELSE 0 END) AS Morphite
FROM invTypeMaterials AS m1
WHERE m1.typeID = 10039

RESULT:

typeID  Tritanium   Pyerite     Mexallon    Isogen  Nocxium     Zydrine     Megacyte    Morphite    
10039   71          24          1           0       0           0           0           0

When i take out the "WHERE" clause and try to pull them all it shows up like this:

 typeID     Tritanium   Pyerite     Mexallon    Isogen  Nocxium     Zydrine     Megacyte    Morphite    
18  13989190531     1301687143  261706229   54807927    14277967    4262960     1540121     40701

Any ideas?

UPDATE 2: I got it, the GROUP BY did the trick

Assuming the typeName values are always the same you can use subqueries in the select section of your query to get the values you want.

$sql = "SELECT m.typeID, 
    (SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Tritanium') as Tritanium,
    (SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Pyerite') as Pyerite,
    (SELECT m.quantity FROM invTypes AS t WHERE m.materialTypeID = t.typeID AND t.typeName = 'Mexallon') as Mexallon
FROM invTypeMaterials AS m
WHERE m.typeID = 10039";

I don't think there's any way in SQL to generate dynamic columns. If you can write dynamic SQL, then yes but in pure SQL, I don't think so.

If you're willing to hard-code the columns then you can do

SELECT
    m.TypeID,
    MAX(CASE
        WHEN t.typeName = 'Tritanium' THEN m.quantity
        ELSE 0
    END) Tritanium,
    MAX(CASE
        WHEN t.typeName = 'Pyerite' THEN m.quantity
        ELSE 0
    END) Pyerite,
    MAX(CASE
        WHEN t.typeName = 'Mexallon' THEN m.quantity
        ELSE 0
    END) Mexallon
FROM
    [your FROM clause]
WHERE
    [your WHERE clause]
GROUP BY
    m.TypeID;
We have pivot clause in oracle to do similar kind of functionality.
It helps in writing cross tabulation query i.e., we can aggregate results and rotate rows into columns.

tried the below with some sample data:

create table types(typeid number,typename varchar2(20),
quantity number);

insert into types values(1,'xx',11);
insert into types values(1,'yy',121);
insert into types values(1,'zz',113);

delete from types where typeid in(2,3);

select *from
(select typeid,typename,quantity from types)
pivot
(
sum(quantity)
for typename in('xx','yy','zz') 
)


typeid      xx      yy     zz
1           11      121    113

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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