mysql多个字段关联查询

mysql多个字段关联查询,第1张

数据结构设置的不太合理,查询效率会比较低,假设表名分别是images和products

请参考下列语句:

select a.thumbnailId,

(select path from images where id=a.thumbnailId)

as thumbnailPath,t. imageId,

t.imagePath from products a

left join

(select b.imageId,group_concat(c.path) as imagePath

from products b,images c

where b.imageId like concat('%',c.id,'%') group by b.imageId) t

on a.imageId=t.imageId

实际测试如下:

测试代码

images源数据

products源数据

输出效果

说明:图片ID要统一ID位数(如试验中全部统一为4位图片ID),输出才准确

用动态执行的sql,类似这样的。 create procedure sp_find(pfind varchar(500) BEGIN DECLAR msql varchar(2000)SET @MyQuery=Concat('select * from 表 where ',pfind)PREPARE msql from @MyQueryEXECUTE msqlEND


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7431984.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-05
下一篇 2023-04-05

发表评论

登录后才能评论

评论列表(0条)

保存