coalesce((select id from table1 where name=left(tt2.name,1)),-1) nameID1,
coalesce((select id from table1 where name=mid(tt2.name,3,1)),-1) nameID2,
coalesce((select id from table1 where name=mid(tt2.name,5,1)),-1) nameID3,
coalesce((select id from table1 where name=mid(tt2.name,7,1)),-1) nameID4
from table2
如果保证table2.name中无空格且table1.name长度都为1,那以下也可以用
select table2.id,table2.name,max(case when instr(table2.name,table1.name)=1 then table1.id else -1 end) naemID1,
max(case when instr(table2.name,table1.name)=3 then table1.id else -1 end) naemID2,
max(case when instr(table2.name,table1.name)=5 then table1.id else -1 end) naemID3,
max(case when instr(table2.name,table1.name)=7 then table1.id else -1 end) naemID4
from table2 left join tt1 on instr(table2.name, table1.name)>0
group by table2.id, table2.name
如果table2.name有空格或table1.name长度不都为1,也可以使用
select table2.id,table2.name,max(case when length(left(table2.name,instr(table2.name, table1.name)))-length(replace(left(table2.name,instr(table2.name,table1.name)), ',', ''))=0
then table1.id else -1 end) naemID1,
max(case when length(left(table2.name,instr(table2.name, table1.name)))-length(replace(left(table2.name,instr(table2.name,table1.name)), ',', ''))=1
then table1.id else -1 end) naemID2,
max(case when length(left(table2.name,instr(table2.name, table1.name)))-length(replace(left(table2.name,instr(table2.name,table1.name)), ',', ''))=2
then table1.id else -1 end) naemID3,
max(case when length(left(table2.name,instr(table2.name, table1.name)))-length(replace(left(table2.name,instr(table2.name,table1.name)), ',', ''))=3
then table1.id else -1 end) naemID4
from table2 left join table1 on instr(table2.name, table1.name)>0
group by table2.id, table2.name
length(left(table2.name,instr(table2.name, table1.name)))-length(replace(left(table2.name,instr(table2.name,table1.name)), ',', ''))这个表达式是找到相应的table1.name前面的逗号个数即对应table1.name的位置
select sum(c.value) from 表1 a,表2 b,表3 cwhere a.pid=b.pid and b.uid=c.uid
and a.mid='机器号'
这种查询没什么效率问题可言,只不过是分步查询与一步查询的关系。其实执行的过程都是一样的
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)