从多值列中查询不同的值

时间:2014-03-17 03:10:05

标签: sql ms-access

我正在尝试查询“标签”列中的所有唯一值。 标签列中的每一行都可以包含多个值。 因此,如果不强制进入规范化,我如何查询多值列?

示例行:

Networking
Professionalism
Time Management
Communication, Networking
Career Management, Professionalism
Networking
Communication
Attitude, Interpersonal Skills, Professionalism
Business Protocol, Career Management, Communication, Leadership
Business Protocol, Networking

1 个答案:

答案 0 :(得分:1)

如果元素的最大数量是可预测的,您可以使用此功能(请注意,您需要使用UNION,而不是UNION ALL

Select DISTINCT thefield from thetable where Instr(thefield, ',') = 0
UNION 
Select Distinct Mid(thefield, 1, Instr(thefield, ',')) from thetable Where len(thefield) - len(replace(thefield,',','')) = 1
UNION 
Select Distinct Mid(thefield, Instr(thefield, ',')+1) from thetable Where len(thefield) - len(replace(thefield,',','')) = 1
UNION 
Select Distinct Mid(thefield, Instr(thefield, ',')+1, Instr(Instr(thefield, ',')+1,thefield, ',')) from thetable Where len(thefield) - len(replace(thefield,',','')) = 2
UNION 
Select Distinct Mid(thefield, Instr(Instr(thefield, ',')+1,thefield, ',')+1) from thetable Where len(thefield) - len(replace(thefield,',','')) = 2
--.. and so on (repeat last two Selects as many time as you wish, increasing the where condition by one for each pair)

看起来有点笨重,但应该做的工作。未经测试,因此,您可能会在值

之前或之后出现迷路逗号
相关问题