首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Conditional MySQL列

Conditional MySQL列
EN

Stack Overflow用户
提问于 2012-07-03 23:01:10
回答 2查看 53关注 0票数 0

表: qa_selltypes

代码语言:javascript
运行
复制
selltype_code, selltype_name
'1', 'unit'
'2', 'pound'
'3', 'box'
'4', 'gallon'

表: qa_items

代码语言:javascript
运行
复制
item_code, item_name, selltype_code, item_price
'1', 'PS2 Keyboard', '1', '10.45'
'2', 'Screws', '3', '15.02'
'3', 'Oil', '2', '30.00'

我需要重新填充一个包含四个字段(单位、停顿、磅、加仑)的网格,根据与selltype_code字段的关系建立item_price,一个示例:其余的将它们设置为0

DGV:

代码语言:javascript
运行
复制
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'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-03 23:07:36

代码语言:javascript
运行
复制
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
票数 3
EN

Stack Overflow用户

发布于 2012-07-03 23:04:52

您可以使用case。示例:

代码语言:javascript
运行
复制
select case when some_column = 'abc'
            then 'hello'
            else 'good bye'
            end as conditional_column
from your_table
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11313749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档