mysql 联查问题 ?

冰糖710 2011-11-25 01:10:56
属性表:

attr_id(自增) attr_name attr_cate_id

1 型号 4
2 重量 4
3 时速 4

商品表:
goods_id goods_cate_id

1 4

商品属性值对应表
goods_attr_id (自增) goods_id attr_value attr_id
1 1 (型号)M9 1
2 1 (重量)1.5kg 2

现在要的结果是:
goods_id attr_name attr_value attr_id
1 型号 M9 1
1 重量 1.5kg 2
1 时速 null 3

我联查始终只有前面有attr_value的最后个时速没得....???

查询结果:

属性全有,属性值没有就为null?

说得我都不晓得啥意思了....
看看吧...
...全文
53 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicenight 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 a710123a 的回复:]

引用 1 楼 nicenight 的回复:
你的语句是否是这样的?

SQL code
select goods_id attr_name attr_value attr_id
from
商品表 as t1
left join 属性表 as t2 on t1.goods_cate_id = t2.attr_cate_id
left join 属性表值对应表 as t3 on t2……
[/Quote]

这个是根据你的目标来做确认的。另外就像说话一样主谓宾定状补,每一个成份都有自己的位置嘛,你的商品是主语自然就要在第一个位置了。
冰糖710 2011-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nicenight 的回复:]
你的语句是否是这样的?

SQL code
select goods_id attr_name attr_value attr_id
from
商品表 as t1
left join 属性表 as t2 on t1.goods_cate_id = t2.attr_cate_id
left join 属性表值对应表 as t3 on t2.attr_id = t3.goods……
[/Quote]

按照你的意思改了一下,试了的确可以,我想不通为啥要以商品表为主...
WWWWA 2011-11-25
  • 打赏
  • 举报
回复
select c.goods_id,a.attr_name,c.attr_value,a1.attr_id from 属性表 a1 left join
(select a.*,b.goods_id,b.goods_cate_id from 商品属性值对应表 a inner join 商品表 b on a.goods_id=b.goods_id) c
on a1.attr_cate_id=c.goods_cate_id
nicenight 2011-11-25
  • 打赏
  • 举报
回复
你的语句是否是这样的?
select goods_id attr_name attr_value attr_id
from
商品表 as t1
left join 属性表 as t2 on t1.goods_cate_id = t2.attr_cate_id
left join 属性表值对应表 as t3 on t2.attr_id = t3.goods_attr_id


结果不应该没有时速吧,可能你把 属性表值对应表 放在 属性表 前面做连接了?

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧