2013-06-10 2 views
0

T SQL에 익숙하지 않아 일반적인 열 값에 따라 조합해야하는 두 가지 쿼리가 있습니다. 두 쿼리가 모두 제대로 작동합니다.SQL 쿼리 결합하기

첫 번째 쿼리가

SELECT t_senderTable.nameFull AS "senderName", t_recieverTable.recieverName AS "recieverName" 
FROM ((dbo.t_senderTable AS t_senderTable 
    INNER JOIN t_senderTable AS t_senderTable ON (t_senderTable.Kd = mapTable.senderID)) 
    INNER JOIN t_recieverTable AS t_recieverTabler ON (recieverTable.Id = mapTable.recieverID) 

두 번째 쿼리가

SELECT t_license AS "License", t_coName AS "Company Name" 
FROM (dbo.t_license AS t_license 
    INNER JOIN dbo. t_coName ON (t_coName.id = t_license.senderID)) 
WHERE 
(
    t_license.check < '2') 

입니다 기본적으로 나는 결합하는 데 필요한 두 개의 쿼리, 센더를 사용하여 두 개의 쿼리에 공통입니다 것을 내가 SENDERNAME의 출력 결과를 얻을 수 있도록, recieverName 및 coName senderID는 일대 다 많은 관계입니다. 이 게시물에서 아이디어를 얻었으나 작동하지 않습니다. Combining SQL Server Queries 아이디어가 있습니까? 감사

+0

: 나는 당신을 위해 무엇을 요구하는 것은 생각합니다. – ioscode

답변

0

두 쿼리를 하위 쿼리로 넣고 두 하위 쿼리의 select 절에 senderId 필드를 포함시킨 다음 해당 값을 조인 할 수 있습니다. 그냥 다음 왼쪽을, 두 쿼리에서 선택한 열 목록에 센더를 추가 당신이 게시 된 링크를 발견처럼 가입하면, 그 일을해야

SELECT q1.senderName 
    , q1.recieverName 
    , q2."Company Name" 
(
SELECT t_senderTable.nameFull AS "senderName", t_recieverTable.recieverName AS "recieverName" 
    , t_senderTable.Id 
FROM ((dbo.t_senderTable AS t_senderTable 
    INNER JOIN t_senderTable AS t_senderTable ON (t_senderTable.Kd = mapTable.senderID)) 
    INNER JOIN t_recieverTable AS t_recieverTabler ON (recieverTable.Id = mapTable.recieverID) 
) q1 
INNER JOIN (
SELECT t_license AS "License", t_coName AS "Company Name" 
    , t_license.senderID 
FROM (dbo.t_license AS t_license 
    INNER JOIN dbo. t_coName ON (t_coName.id = t_license.senderID)) 
WHERE 
(
    t_license.check < '2') 
) q2 
    ON q1.Id = q2.senderId 
1

UNION (중복 행 제거) 또는 UNION ALL (모든 행 반환)을 사용할 수 있습니다. 작동 시키려면 두 열 모두에서 열을 일치시켜야합니다.

+0

@ J.Davidson : 이것에 대한 메모 - SQL에'TEXT' 또는'NTEXT' 또는 이와 유사한 큰 필드가 있다면'UNION ALL'을 사용해야합니다. – Paul

관련 문제