我如何绑定两个TBL(由2个选择查询返回) [英] how can i bind two tbls (returning by 2 select queries)

查看:66
本文介绍了我如何绑定两个TBL(由2个选择查询返回)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何绑定两个tbl(通过2个选择查询返回)

在单个tbl中检索所有这些数据(列),
我的意思是在单个tbl中检索所有这些数据,其中包含5个列而不是4个列(pname为2nd select查询)

how can i bind two tbls (returning by 2 select queries)
or
retrieve all these data(columns) in single tbl,
i mean that retrieve all these data in single tbl containing 5 coumns instead of 4 (with pname of 2nd select query)

create proc [dbo].[SpCateProducts](@cate_id int)
    as
begin
    select sb.subcate_name, sum(p.qty)
    from subcategory sb 
      join product p on p.subcate_id = sb.subcate_id
    where sb.cate_id = @cate_id
    group by sb.subcate_name

        select top 1 pname from product join subcategory
    on product.subcate_id=
    (
        select top 1 subcate_id
        from subcategory
        where cate_id=3
        order by NEWID()
    )
end

推荐答案

没有表模式(我记得您先前的问题 [ ^ ] ,但仍然...)及其关系,以及您确切想要的规范很难回答(绑定两个tbls"甚至都不是英语-尽管事实在sql中没有意义).
不过,我仍然有您想要的想法.尝试在第一个查询中添加子查询:

Without the table schema (I remember your previous question[^], but still...) and their relations, and the specification of what you want exactly it is difficult to answer ("bind two tbls" is not even in English - despite the fact that has no meaning in sql).
Still, I have my ideas what you want. Try adding a subquery to your first one:

select sb.subcate_name, sum(p.qty), (select pr.p_name from product pr where pr.subcate_id = sb.subcate_id) as p_name
    from subcategory sb 
      join product p on p.subcate_id = sb.subcate_id
    where sb.cate_id = @cate_id
    group by sb.subcate_name


这篇关于我如何绑定两个TBL(由2个选择查询返回)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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