sql多表查询语句,显示不重复数据
首先是写在sql的存储过程中,然后我有两个表,其一是文章,其二是留言,我要把这两个表连接起来查询,我的语句是:ALTERPROCEDURE[dbo].[pro_GetTe...
首先是写在sql的存储过程中,然后我有两个表,其一是文章, 其二是留言,我要把这两个表连接起来查询,我的语句是:
ALTER PROCEDURE [dbo].[pro_GetText_One]
@firId int,
@secId int
AS
BEGIN
SELECT distinct text_Text.tId,text_Discuss.SendTime FROM text_Discuss inner JOIN
text_Text ON text_Discuss.tId = text_Text.tId where text_Text.firId=@firId and text_Text.secId=@secId and text_Text.tId in(select distinct tId from text_Text)
END
可查询出的还是重复的数据,想问下该怎么改呢?
就是每个重复的都只显示一次 展开
ALTER PROCEDURE [dbo].[pro_GetText_One]
@firId int,
@secId int
AS
BEGIN
SELECT distinct text_Text.tId,text_Discuss.SendTime FROM text_Discuss inner JOIN
text_Text ON text_Discuss.tId = text_Text.tId where text_Text.firId=@firId and text_Text.secId=@secId and text_Text.tId in(select distinct tId from text_Text)
END
可查询出的还是重复的数据,想问下该怎么改呢?
就是每个重复的都只显示一次 展开
6个回答
展开全部
SELECT T1.name,T1.buyAmount,T2.sellAmount FROM
(SELECT A.name,SUM(buyAmount) FROM A GROUP BY A.name) T1
LEFT JOIN
(SELECT B.name,SUM(sellAmount) FROM B GROUP BY B.name) T2
ON T1.name=T2.name
答案应该是这样子。先统计后联接
(SELECT A.name,SUM(buyAmount) FROM A GROUP BY A.name) T1
LEFT JOIN
(SELECT B.name,SUM(sellAmount) FROM B GROUP BY B.name) T2
ON T1.name=T2.name
答案应该是这样子。先统计后联接
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sql语句1:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql语句2:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql语句3:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql语句4:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
这四条语句的执行结果一样的,只是在大数据的时候效率会不一样,
你可以把where 换成and试试,或者直接用where 写了,不要用inner join ..on了
sql语句2:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql语句3:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql语句4:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
这四条语句的执行结果一样的,只是在大数据的时候效率会不一样,
你可以把where 换成and试试,或者直接用where 写了,不要用inner join ..on了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这得看你要哪些字段的数据了
你要明白,这两表的关系是一对多的关系,所以在查询的时候如果两表字段都有出现,必然会有重复的
你要明白,这两表的关系是一对多的关系,所以在查询的时候如果两表字段都有出现,必然会有重复的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接从两个表里面查吧,后面写条件
SELECT distinct text_Text.tId,text_Discuss.SendTime FROM text_Discuss ,text_Text where text_Discuss.tId = text_Text.tId andtext_Text.firId
SELECT distinct text_Text.tId,text_Discuss.SendTime FROM text_Discuss ,text_Text where text_Discuss.tId = text_Text.tId andtext_Text.firId
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT distinct text_Text.tId,text_Discuss.SendTime FROM text_Discuss inner JOIN
text_Text ON text_Discuss.tId = text_Text.tId where text_Text.firId=@firId and text_Text.secId=@secId
text_Text ON text_Discuss.tId = text_Text.tId where text_Text.firId=@firId and text_Text.secId=@secId
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询