2012-07-03 4 views
0

표 :조건부 MySQL의 열

selltype_code, selltype_name 
'1', 'unit' 
'2', 'pound' 
'3', 'box' 
'4', 'gallon' 

표를 qa_selltypes 다음에 따라 ITEM_PRICE 수립 나는 네 개의 필드 (단위, 매점, 파운드, 갤런)를 포함하는 그리드를 보충 할 필요가

item_code, item_name, selltype_code, item_price 
'1', 'PS2 Keyboard', '1', '10.45' 
'2', 'Screws', '3', '15.02' 
'3', 'Oil', '2', '30.00' 

qa_items selltype_code 필드와의 관계 : 나머지는 0으로 설정합니다.

DGV :

item_code, item_name, unit, box, pound, gallon 
'1', 'PS2 Keyboard', '10.45', '0', '0', '0' 
'2', 'Screws', '0', '0', '15.02', '0' 
'3', 'Oil', '0', '30.00', '0', '0' 
+0

사용 CASE 문, http://dev.mysql.com/doc/refman/5.0/en/case-statement.html –

답변

3
select i.item_code, i.item_name, 
    case when q.selltype_name = 'unit' then i.item_price else 0 end as unit, 
    case when q.selltype_name = 'box' then i.item_price else 0 end as box, 
    case when q.selltype_name = 'pound' then i.item_price else 0 end as pound, 
    case when q.selltype_name = 'gallon' then i.item_price else 0 end as gallon 
from qa_items i 
inner join qa_selltypes q on i.selltype_code = q.selltype_code 
1

case을 사용할 수 있습니다. 예 :

select case when some_column = 'abc' 
      then 'hello' 
      else 'good bye' 
      end as conditional_column 
from your_table