Mysql 找到字符串位置并连接另一个表';s排
我有两个关于得到两套表格的主要问题Mysql 找到字符串位置并连接另一个表';s排,mysql,string,position,inner-join,Mysql,String,Position,Inner Join,我有两个关于得到两套表格的主要问题 table : room_type rt_id rt_title 1 Type A 2 Type B 3 Type C 4 Type D 5 Type E 这是另一张桌子 表:费率/成本 id rate hotel cost date 2999 7 1 4700-5400-6100-6600-7300 2012-11-01 3
table : room_type
rt_id rt_title
1 Type A
2 Type B
3 Type C
4 Type D
5 Type E
这是另一张桌子
表:费率/成本
id rate hotel cost date
2999 7 1 4700-5400-6100-6600-7300 2012-11-01
3000 7 1 4700-5400-6100-6600-7300 2012-11-02
3001 7 1 4700-5400-6100-6600-7300 2012-11-03
3002 7 1 4700-5400-6100-6600-7300 2012-11-04
3003 7 1 4700-5400-6100-6600-7300 2012-11-05
3004 7 1 4700-5400-6100-6600-7300 2012-11-06
3005 7 1 4700-5400-6100-6600-7300 2012-11-07
3006 7 1 4700-5400-6100-6600-7300 2012-11-08
3007 7 1 4700-5400-6100-6600-7300 2012-11-09
3008 7 1 4700-5400-6100-6600-7300 2012-11-10
3009 7 1 4700-5400-6100-6600-7300 2012-11-11
3010 7 1 4700-5400-6100-6600-7300 2012-11-12
3011 7 1 4700-5400-6100-6600-7300 2012-11-13
3012 7 1 4700-5400-6100-6600-7300 2012-11-14
3013 7 1 4700-5400-6100-6600-7300 2012-11-15
3014 7 1 4700-5400-6100-6600-7300 2012-11-16
3015 7 1 4700-5400-6100-6600-7300 2012-11-17
3016 7 1 4700-5400-6100-6600-7300 2012-11-18
3017 7 1 4700-5400-6100-6600-7300 2012-11-19
3018 7 1 4700-5400-6100-6600-7300 2012-11-20
3019 7 1 4700-5400-6100-6600-7300 2012-11-21
3020 7 1 4700-5400-6100-6600-7300 2012-11-22
3021 7 1 4700-5400-6100-6600-7300 2012-11-23
3022 7 1 4700-5400-6100-6600-7300 2012-11-24
3023 7 1 4700-5400-6100-6600-7300 2012-11-25
3024 7 1 4700-5400-6100-6600-7300 2012-11-26
3025 7 1 4700-5400-6100-6600-7300 2012-11-27
3026 7 1 4700-5400-6100-6600-7300 2012-11-28
3027 7 1 4700-5400-6100-6600-7300 2012-11-29
3028 7 1 4700-5400-6100-6600-7300 2012-11-30
3028 8 2 4700-5400-6100-6600-7300 2012-11-01
3028 9 3 4700-5400-6100-6600-7300 2012-11-01
每个以破折号分隔的数字是根据其位置针对每个房间类型的费率成本。例如,4700是a类(rt_id=1)的费率成本,5400是B类。。。7300为E型(rt_id=5)
现在我想创建一个如下的结果
Day/Room type Type A | Type B | Type C | Type D | Type E
1 Nov 2012 4700 5400 6100 6600 7300
2 Nov 2012 4700 5400 6100 6600 7300
3 Nov 2012 4700 5400 6100 6600 7300
4 Nov 2012 4700 5400 6100 6600 7300
...
30 Nov 2012 4700 5400 6100 6600 7300
我在PHP中完成了这些结果,但它需要几秒钟才能完成。我试着自己做,但我缺乏mySQL知识。所以请告诉我。将字符串拆分为列:
select date,
substring_index(cost, '-', 1) type_a,
case when cost regexp '.*-' then
substring_index(substring_index(cost, '-', 2), '-', -1)
else ''
end type_b,
case when cost regexp '.*-.*-' then
substring_index(substring_index(cost, '-', 3), '-', -1)
else ''
end type_c,
case when cost regexp '.*-.*-.*-' then
substring_index(substring_index(cost, '-', 4), '-', -1)
else ''
end type_d,
case when cost regexp '.*-.*-.*-.*-' then
substring_index(substring_index(cost, '-', 5), '-', -1)
else ''
end type_e
from rate_cost;
如果可以修改表设计,最好创建多个列:
create table rate_cost (
id int,
rate int,
hotel int,
cost_type_a int,
cost_type_b int,
cost_type_c int,
cost_type_d int,
cost_type_e int,
date date);
你真的应该改变你的数据库结构。规范化您的数据以避免您现在遇到的巨大问题。@juergend。现在我才意识到我的数据库结构很难使用。但出于某种原因,我现在真的需要通过这个问题。特别是在时间限制方面:(这个原因是开发人员的终身伴侣。不要认为你会有时间更改它。无论如何,你应该真正更改它。这将在将来为你节省大量的时间。@juergend。谢谢你的重新声明,但现在我需要解决它。我无论如何都要更改它,但不是立即更改。如果我的列少于5,该怎么办?是的我非常同意我应该规范我的数据库结构。但现在不行。你是什么意思?你总是少于5列,还是每行的列数不同?有时一些酒店。例如代码4或5可能有2或3个费率:
hotel 4=2500-2700 hotel 5=300-3200-3500
最后一列将重复:酒店4=2500、2700、2700、2700、2700、2700;酒店5=300、3200、3500、3500、3500。如果您想要空列,您必须测试您有多少破折号。这将变得非常难看。尝试修改后的答案,当价格低于5时,它应该提供空列。