【问题标题】:how to use inner join and sub query at a time?如何一次使用内连接和子查询?
【发布时间】:2017-07-10 21:27:12
【问题描述】:

我创建了 3 个表。 第一个巡回表 第二个 tour_details 表 第三个 tour_img 表

游览表

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)
)

现在,我想使用内部连接来连接这三个表,并且只需要 tour_img 表的 1 行

举例

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';

【问题讨论】:

    标签: sql asp.net sql-server asp.net-mvc asp.net-mvc-4


    【解决方案1】:
    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 加入到外部查询中。

      【讨论】:

      • 谢谢你,先生.. :)
      • 它正在工作:)
      • 您可能需要考虑此处的其他方法(加入或交叉应用)。此方法将导致 N+1。如果只有一条记录就可以了,否则可能会自找麻烦
      【解决方案3】:

      我交叉申请

      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';
      

      【讨论】:

        【解决方案4】:

        您可以使用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_id 的“最后一个”tour_img。如果您想要第一个,您可以在CROSS APPLYORDER BY 中执行ORDER BY tour_img ASC,无论您想要什么条件。

        您还可以在CROSS APPLYWHERE 子句中添加一些内容,以便根据特殊情况为每个游览获取特定行。

        【讨论】:

          猜你喜欢
          • 2021-05-13
          • 1970-01-01
          • 2021-09-25
          • 1970-01-01
          • 2013-04-16
          • 2022-01-20
          • 2019-06-17
          • 2023-03-18
          • 1970-01-01
          相关资源
          最近更新 更多