SQL联接查询无法正常工作 [英] sql join query not working correctly

查看:131
本文介绍了SQL联接查询无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

USE [cms]
GO
/****** Object:  StoredProcedure [dbo].[SpGetRelatedProducts]    Script Date: 10/10/2012 23:55:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SpGetRelatedProducts](@pid int)
AS
select *
from product join subcategory on subcategory.subcate_id=category.cate_id
where product.subcate_id in (
                              select subcate_id 
                              from product 
                              where pid = @pid
                            )




我希望所有产品属于不同的子类别,这些子类别属于同一类别,也属于子类别表的列,这就是我在上面使用join的原因,但是查询中出了点问题.

加入 子类别.subcate_id= category.cate_id



 来自产品 join 子类别  subcategory.subcate_id = product.subcate_id 


并删除您的where子句,并将其替换为:

 其中 pid =  @ pid  


USE [cms]
GO
/****** Object:  StoredProcedure [dbo].[SpGetRelatedProducts]    Script Date: 10/10/2012 23:55:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SpGetRelatedProducts](@pid int)
AS
select *
from product join subcategory on subcategory.subcate_id=category.cate_id
where product.subcate_id in (
                              select subcate_id 
                              from product 
                              where pid = @pid
                            )




i want that all the products, which belong from different subcategory and these subcategories belongs from same category and also the columns of the subcategory table, thats why i used above join, but there is something wrong in the query.

解决方案

change

from product join subcategory on subcategory.subcate_id=category.cate_id


to

from product join subcategory on subcategory.subcate_id=product.subcate_id


and remove your where clause and replace it with:

where pid = @pid


这篇关于SQL联接查询无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆