表t_unit
code unit_name
01 毫克
02 毫升
03 克表t_item
id item_name weight weight_unit(重量单位) volum volum_unit(体积单位)
1 品名1 0.1 01 NULL NULL
2 品名2 NULL NULL 0.2 02
3 品名3 0.3 03 0.5 02请问我想要这样的结果
id item_name weight weight_unit volum volum_unit
1 品名1 0.1 毫克 NULL NULL
2 品名2 NULL NULL 0.2 毫升
3 品名3 0.3 克 0.5 毫升
语句怎么写?
谢谢
code unit_name
01 毫克
02 毫升
03 克表t_item
id item_name weight weight_unit(重量单位) volum volum_unit(体积单位)
1 品名1 0.1 01 NULL NULL
2 品名2 NULL NULL 0.2 02
3 品名3 0.3 03 0.5 02请问我想要这样的结果
id item_name weight weight_unit volum volum_unit
1 品名1 0.1 毫克 NULL NULL
2 品名2 NULL NULL 0.2 毫升
3 品名3 0.3 克 0.5 毫升
语句怎么写?
谢谢
*
from
t_unit a join t_item b
on
a.unit_name=b.unit_name
主要是t_item的两个单位字段要关联到t_unit的id然后取unit_name
这个是别人程序设置的表,我没办法去改,所以想知道有没有办法
谢谢··
if object_id('[t_unit]') is not null drop table [t_unit]
go
create table [t_unit] (code varchar(2),unit_name varchar(4))
insert into [t_unit]
select '01','毫克' union all
select '02','毫升' union all
select '03','克'
--> 测试数据: [t_item]
if object_id('[t_item]') is not null drop table [t_item]
go
create table [t_item] (id int,item_name varchar(5),weight numeric(2,1),weight_unit varchar(2),volum numeric(2,1),volum_unit varchar(2))
insert into [t_item]
select 1,'品名1',0.1,'01',null,null union all
select 2,'品名2',null,null,0.2,'02' union all
select 3,'品名3',0.3,'03',0.5,'02'select id ,
item_name,
weight,
weight_unit=a.unit_name,
volum,
volum_unit=b.unit_name
from [t_item]
left join [t_unit] a on weight_unit=a.code
left join [t_unit] b on volum_unit=b.codeid item_name weight weight_unit volum volum_unit
----------- --------- --------------------------------------- ----------- --------------------------------------- ----------
1 品名1 0.1 毫克 NULL NULL
2 品名2 NULL NULL 0.2 毫升
3 品名3 0.3 克 0.5 毫升(3 行受影响)
你的t_item表里面存在区分的字段。
不好意思。weight_unit(重量单位)
volum_unit(体积单位)