简体   繁体   中英

mysql query; rename 2 values from single column

I have a table like this. I am trying to query values from column id5 and give them new names.

 id      id2   id3 id4   id5
----------------------------
174977  20214 209 8278  1
174976  20214 209 8277  abc123
174975  20214 209 8276  33333
174974  20214 209 8275  My Name

here is my query

select a.id5 as qty, b.id5 as itemnum 
from mytable a where id4 = 8278, 
mytable b where id4 = 8276

so that I get an output like this.

qty      itemnum  
----------------
1        33333

Can anyone help please? Thank you

SELECT MAX(CASE WHEN id4 = 8278 THEN id5 END) qty
     , MAX(CASE WHEN id4 = 8276 THEN id5 END) itemnum
  FROM mytable
 GROUP 
    BY id2
     , id3;

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