如何使用SQL Select查询从两个表中获取不同的数据

时间:2017-03-30 10:08:09

标签: sql-server sql-server-2008

如何使用SQL Select查询从两个表中获取不同的数据。我需要知道有多少用户开始创新但需要不同的数据。以下是两个表格。 Table_UserInformation是基本注册表,登录后他可以添加许多创新,但只能为一个电子邮件ID注册一个。

Table_UserInformation

Name      Email             mobile
mazhar    mazar@gmail.com   720000000
rahul     rahul@gmail.com   992000000
raj       raj@gmail.com     700000000

Table_Innovation

Email             Data1                Data2
mazar@gmail.com   aim to achive LD     NA
mazar@gmail.com   Search for LD        NA
rahul@gmail.com   mind tech            Techno

我想知道有多少用户开始创新计数和详细信息,如下面2输出需要。

  1. 输出:有多少用户开始创新基于电子邮件需求的数量。

    counts
     2
    
  2. 输出:启动了多少用户基于电子邮件的创新需要用户的详细信息。

     Name      Email             mobile
     mazhar    mazar@gmail.com   720000000
     rahul    rahul@gmail.com   992000000
    

4 个答案:

答案 0 :(得分:0)

您可以使用不同的计数并存在这些查询

对于计数:

select count(distinct Email) from Table_innovation

针对不同的用户

select * from Table_UserInformation t where exists (select 1 from Table_innovation i where i.email = t.email)

答案 1 :(得分:0)

尝试以下查询,这将为您提供相关提及的所需输出。

java.awt.Desktop.desktop.open(f)

<强>输出

DECLARE @Table_UserInformation TABLE(Name  varchar(100),    Email         varchar(100),        mobile varchar(100))
DECLARE @Table_Innovation TABLE(Email  varchar(100),    DATA1         varchar(100),        DATA2 varchar(100))
INSERT INTO @Table_UserInformation VALUES
('mazhar',    'mazar@gmail.com',   '720000000'),
('rahul',    'rahul@gmail.com',   '992000000'),
('raj',       'raj@gmail.com',     '700000000')
INSERT INTO @Table_Innovation VALUES
('mazar@gmail.com',   'aim to achive LD',     'NA'),
('mazar@gmail.com',   'Search for LD',        'NA'),
('rahul@gmail.com',   'mind tech',            'Techno')
;WITH SAMPLEDATA
AS
(
SELECT Email,COUNT(EMAIL)COUNTS FROM @Table_Innovation GROUP BY Email
)
SELECT T2.Name,T2.Email,T2.mobile,T1.COUNTS FROM SAMPLEDATA T1 JOIN     @Table_UserInformation T2 
ON T1.Email=T2.Email

对两者分开查询:

-----------------------------------------
--Name  Email   mobile  COUNTS
-----------------------------------------
mazhar  mazar@gmail.com 720000000   2
rahul   rahul@gmail.com 992000000   1
-----------------------------------------

输出

SELECT Email,COUNT(EMAIL)COUNTS FROM @Table_Innovation GROUP BY Email

;WITH SAMPLEDATA
AS
(
SELECT Email,COUNT(EMAIL)COUNTS FROM @Table_Innovation GROUP BY Email
)
SELECT T2.Name,T2.Email,T2.mobile FROM SAMPLEDATA T1 JOIN     @Table_UserInformation T2 
ON T1.Email=T2.Email

答案 2 :(得分:0)

您可以尝试这样的事情:

  SELECT  
   a.[Name]
  ,a.[Email]
  ,a.[Mobile]
  ,(select count(b.Email) from Table_Innovation as b where b.Email = a.Email)
    as 'Innovat ion Count'
 FROM Table_UserInformation as a

你的结果应该是这样的: Result

格尔茨 K4ckr31z

答案 3 :(得分:0)

请尝试使用以下代码获取所需的输出:

    DECLARE @Table_UserInformation TABLE(Name VARCHAR(100),Email VARCHAR(100),mobile VARCHAR(100))
    INSERT INTO @Table_UserInformation VALUES
    ('mazhar','mazar@gmail.com','720000000'),
    ('rahul','rahul@gmail.com','992000000'),
    ('raj','raj@gmail.com','700000000')

    DECLARE @Table_Innovation TABLE(Email VARCHAR(100),DATA1 VARCHAR(100),DATA2 VARCHAR(100))
    INSERT INTO @Table_Innovation VALUES
    ('mazar@gmail.com','aim to achive LD','NA'),
    ('mazar@gmail.com','Search for LD','NA'),
    ('rahul@gmail.com','mind tech','Techno')

    SELECT 
        COUNT(DISTINCT Email) AS COUNTS 
    FROM 
        @Table_Innovation

    SELECT 
        DISTINCT T1.Name,T1.Email,T1.mobile
    FROM 
        @Table_UserInformation T1  INNER JOIN @Table_Innovation T2 ON T1.Email=T2.Email