2017-02-21 2 views
0

Я создал 3 таблицы. первый стол тур 2-й tour_details стол третьего tour_img столКак использовать внутреннее соединение и дополнительный запрос за раз?

Tour стол

Create table tour(
id int identity(1,1), 
unique_code varchar(10), 
tour_name varchar(10) 
) 

Tour_details Таблица

Create table tour_details(
id int identity(1,1), 
tour_id varchar(10), 
description varchar(10) 
) 

Tour_img Таблица

Create table tour_img(
id int identity(1,1), 
tour_id varchar(10), 
img_path varchar(max) 
) 

Теперь, я хочу присоединиться к тем, T рите таблицы с использованием внутреннего соединения и хотят только 1 строку таблицы tour_img

Для примера

tour table has 1 record 
id=1, 
unique_code=123456 
tour_name= taj mahal 

Tour_details 
id=1, 
tour_id=123456 
desc=xyz 

Tour_img 
id=1, 
tour_id=123456 
tour_img=taj_01 

id=2, 
tour_id=123456 
tour_img=taj_2 

etc... 

Так я хочу только одну записи из tour_img

Я создал запрос, но это не работает должным образом.

select a.tour_name, 
     b.tour_desc, 
     (
      select tour_img 
      from tour_img 
      where tour_id='123456' 
     ) as tour_img 
from tour a 
inner join tour_details b on a.unique_code = b.tour_id 
inner join tour_img c on a.unique_code = c.tour_id 
where a.unique_code = '123456'; 

ответ

0
select a.tour_name 
    , b.tour_desc 
    , c.tour_img 
    from tour a 
    inner join tour_details b on a.unique_code = b.tour_id 
    inner join tour_img c on a.unique_code = c.tour_id 
    where a.unique_code = '123456'; 
2

Я думаю, что вы ищете что-то вроде этого:

SELECT a.tour_name, 
     b.tour_desc, 

    (SELECT top 1 tour_img 
    FROM tour_img 
    WHERE tour_id = a.unique_code 
    ORDER BY id) AS tour_img 
FROM tour a 
INNER JOIN tour_details b ON a.unique_code = b.tour_id 
WHERE a.unique_code = '123456'; 

Нет необходимости присоединиться к tour_img внешнего запроса.

+0

Спасибо, сэр .. :) –

+0

Это рабочая :) –

+0

[Рад помочь: -)] (http://meta.stackoverflow.com/questions/291325/how-to-show-appreciation-to-a -user-on-stackoverflow/291327 # 291327) –

0

Я пересекаю применять

select t.tour_name, 
     td.tour_desc, 
     ti.tour_img 
from tour t 
     inner join tour_details td 
       on t.unique_code = td.tour_id 
     cross apply (select TOP 1 tour_img 
        from tour_img 
        where tour_id = t.unique_code 
        order by id) ti 
where t.unique_code = '123456'; 
0

Вы можете легко решить эту проблему с помощью CROSS APPLY, как таковой:

SELECT 
    a.[tour_name] 
    ,b.[tour_desc] 
    ,c.[tour_img] 
FROM [tour] AS a 
INNER JOIN [tour_details] AS b 
    ON a.[unique_code] = b.[tour_id] 
CROSS APPLY 
(
    SELECT TOP 1 
    [tour_img] 
    FROM [tour_img] 
    WHERE [tour_id] = a.[unique_code] 
    ORDER BY [tour_id] DESC 
) AS c 
WHERE a.[unique_code] = '12345' 
; 

Это CROSS APPLY должно дать вам "последний" tour_img по tour_id. Если вы хотите первый, вы можете сделать ORDER BY tour_img ASC в CROSS APPLY, или ORDER BY в зависимости от того, что вы хотите.

Вы также можете добавить что-то в пункт WHERECROSS APPLY, чтобы получить конкретную строку для каждого тура, в зависимости от специального состояния.

Смежные вопросы