简体   繁体   English

将表更改为水平的正确 mysql 代码

[英]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我也尝试过 group_concat,我认为这是我需要的,但无法正确设置列

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:当我取出“WHERE”子句并尝试将它们全部拉出时,它显示如下:

 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更新 2:我明白了,GROUP BY 成功了

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.假设typeName值始终相同,您可以在查询的选择部分使用子查询来获取所需的值。

$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.我认为 SQL 中没有任何方法可以生成动态列。 If you can write dynamic SQL, then yes but in pure SQL, I don't think so.如果您可以编写动态 SQL,那么可以,但是在纯 SQL 中,我不这么认为。

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

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

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