如何在不同的表中使用计数 [英] how to use count in different tables

查看:87
本文介绍了如何在不同的表中使用计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要一些帮助.
我有两个表(userticket和adminticketreply).
如果用户生成一张票证并且管理员在同一票证上放置了一些答复.
那么我如何才能从同一票证编号的两个表中获取行数.
两个表都包含ticketnumber列.

userticket

Hello,

i need some help.
i have two tables(userticket and adminticketreply).
if user generate one ticket and admin put some reply on same ticket.
then how can i get number of rows from both the table on same ticketnumber.
both table contain ticketnumber column.

userticket

------------------------------------------
ticketnumber                        message
-----------------------------------------------
   20                                hello
   21                                miss u
   22                                hye
------------------------------------------------



表示用户生成了编号为20的票证并发送给admin,然后admin查看该票证和
自动获得票证编号20,然后他将回复发送给用户.

adminticketreply



means user generate a ticket with number 20 and send to admin and then admin see the ticket and
got the ticket number 20 automatically.and then he send reply to user.

adminticketreply

------------------------------------------
ticketnumber                         reply     
-----------------------------------------------
20                                    welcome
20                                    i m urs
20                                    wanna meet
21                                    see yaa
22                                    byeeee
22                                    happy
20                                    time is coming
----------------------------------------------



然后在两个表中,票证号都是20.
我想将两个表中的两个数字行都添加到同一ticketnumber上.
意味着在对两个票证上相同票号20的两个表中的行数进行计数后,答案将为5. 所以我想知道如何计算两个表中的两个行.



我尝试用它来计数,但没有通过它得到确切的数字.



then in both the table,the ticketnumber is 20.
i would like to add both the number rows in both the table on same ticketnumber.
Means the answer would come 5 after count the number of rows from both table on same ticket number 20.
so i want to know how to count both the rows from both the table.



i tried count with but not get exact number through it.

推荐答案

不确定您的意思,但我认为类似以下的方法应该可以解决问题...

Not sure what you mean, but I think something like the following should do the trick...

declare @ticketnumber as (big)int -- Whatever type you use.
set @ticketnumber = 20 -- This probably is passed as a parameter to an SP.
declare @count1 as int
declare @count2 as int

select
@count1 = COUNT(*)
from userticket
where ticketnumber = @ticketnumber

select
@count2 = COUNT(*)
from adminticketreply
where ticketnumber = @ticketnumber

select 'count' = @count1 + @count2


我不知道这是否是干净"的解决方案,但它对我有用.


I don''t know if it is a ''clean'' solution, but it works for me.


这篇关于如何在不同的表中使用计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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