DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: webb123
今日帖子: 3
在线用户: 1
导航: 论坛 -> 数据库专区 斑竹:liumazi,waterstone  
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/5/27 16:58:46
标题:
求助,表内更新语句 浏览:1129
加入我的收藏
楼主: 如图中显示,需要将class_id改成对应的product_id
比如第二行:
class_id:000077000002,
class_id为000077的product_id=2032
class_id为000077000002的product_id=4
要得到的结果是:1-2032-4(1-可以随后再加),如何批量的更新到Y2,Y3字段里

也就是说当class_id的值是 000077000002时 要分别找到000077的product_id值,跟
000077000002的product_id的值,他们都在本表中

把class_id前6位对应的product_id值更新到Y2字段里,把class_id前12位对应的
product_id值对应的product_id值更新到Y3字段里

求方法,求语句,谢谢!
此帖子包含附件:
PNG 图像
大小:14.0K
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2022/5/27 18:18:50
1楼: 1.select t1.product_id,t1.classId,t2.product_id ,t2.classid from table t1 left join table t2 on substr(t1.classid,1,length(t2.classid))=t2.classId

2.对上面结果按length(t2.classid)排序,t2.product_id 做行转列(oracle也可以用listagg,就直接出字符串了)
----------------------------------------------
-
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/5/28 0:39:47
3楼: wk_knife,您好:

select t2.product_id ,t1.product_id,t1.class_id,t2.class_id
from Products t1 
left join Products t2 on left(t1.class_id,6)=t2.class_id


问题是怎么把查询到的t2.product_id 更新到对应的Y2字段里呢?
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/5/28 1:54:49
4楼: some like this?

FIREBID v3:

update new_table
set Y2 = 555     --- any value or another field:  set Y2 = prod_id

where class_id in (

select DISTINCT class_id from new_table t1
where substring(class_id FROM 1 FOR 6) in (select DISTINCT parent_id from new_table t2)

--COMMENT ... in ('000077', '000088')
--COMMENT: ... and char_length(parent_id)>6

order by parent_id
)



...
commit

NOTE: set fieldxxx =  ( .... )

SET FieldXXXX =  ( SELECT FIRST 1 XXX from ZZZZ ... ) ==> 1 record just
SET FieldXXXX =  ( SELECT FIRST 1 SKIP 2  XXX from ZZZZ ... )  ==> 1 record just, jump 2 record
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/5/28 1:58:33
5楼: note:
The use of "IN" (list...) is not advisable due to the number of items that can be listed in a select!

perhaps it would be more interesting to use "CTE - COMMON TABLE RECURSIVE" technique to select the records and use in "UPDATE"
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/5/28 2:14:04
6楼: my tip!

YOU HAVE ALL VALUE already in CLASS_ID, then, you can just to do a "WHERE" in this field

select XXXX from ZZZZ where class_id = a value
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2022/5/28 11:08:53
7楼: 1.先获取对应的product-id:
select t2.product_id ,t1.product_id,t1.class_id,length(t2.class_id) t2len
from Products t1 
left join Products t2 on left(t1.class_id,length(t2.class_id))=t2.class_id

这样可以得出诸如一个结果集:
t1.product_id,t1.class_id,6,t2.product_id, --6位对应的t2.product_id
t1.product_id,t1.class_id,12,t2.product_id, -12位对应的t2.product_id
t1.product_id,t1.class_id,18,t2.product_id, --18位对应的t2.product_id
.......
t1.product_id,t1.class_id,6*n,t2.product_id, --6n位对应的t2.product_id

2. 按 t2len的数排正序,然后按t1.class_id分组,并进行行转列操作。行转列不同的数据库有不同的做法。参见:https://blog.csdn.net/qq_43288259/article/details/114283482
----------------------------------------------
-
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/5/28 12:29:40
8楼: --第一级
update products  set products.y2=p2.product_id 
from products inner join products as p2 on LEFT(products.class_id,6)=p2.class_id 

--第二级
update products  set products.y3=p2.product_id 
from products inner join productss as p2 on LEFT(products.class_id,12)=p2.class_id 
where len(products.class_id)>6
--第三级
update products  set products.y4=p2.product_id 
from products inner join productss as p2 on LEFT(products.class_id,18)=p2.class_id 
where len(products.class_id)>12
--第四级
update products  set products.y5=p2.product_id 
from products inner join productss as p2 on LEFT(products.class_id,24)=p2.class_id 
where len(products.class_id)>18
--第五级
update products  set products.y6=p2.product_id 
from products inner join productss as p2 on LEFT(products.class_id,30)=p2.class_id 
where len(products.class_id)>24

--组装  需要依次执行下方4个update
update products set y6=''

select * from products

update products set y6=Y1+Y2 where Y2<>''
update products set y6=Y6+'-'+Y3 where Y3<>''
update products set y6=Y6+'-'+Y4 where Y4<>''
update products set y6=Y6+'-'+Y5 where Y5<>''
此帖子包含附件:
PNG 图像
大小:36.6K
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
作者:
男 sxfgf (FC_FGF) ★☆☆☆☆ -
普通会员
2022/5/28 12:32:30
9楼: 感谢大家的支持,谢谢!
没办法,办法虽然很low,但也解决了问题

这是从一个软件中的商品信息(包含商品分类)导入到另外一个软件的分类信息的转换,客户商品1W以上,重新分类工作量太大了

再次感谢大家!
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行119.1406毫秒 RSS