使用带有连接的子查询

时间:2017-10-09 17:25:50

标签: sql-server sql-server-2014

我试图获取已在我们的场地预订国家/地区节目的客户列表,但我似乎无法获得子查询和IN过滤器。

SELECT Customers.CustFirstName, Customers.CustLastName
FROM Customers 
WHERE Customers.CustomerID IN 
(SELECT  Engagements.CustomerID, Musical_Styles.StyleName
from Engagements
inner join Entertainers
ON engagements.EntertainerID = Entertainers.EntertainerID
inner join Entertainer_Styles
ON Entertainers.EntertainerID = Entertainer_Styles.EntertainerID
INNER JOIN Musical_Styles
ON Entertainer_Styles.StyleID = Musical_Styles.StyleID
WHERE Musical_Styles.StyleName = 'Country')

2 个答案:

答案 0 :(得分:1)

SELECT customers.customerid 
FROM   customers 
JOIN   
       ( 
                  select     engagements.customerid 
                  FROM       engagements 
                  INNER JOIN entertainers 
                  ON         engagements.entertainerid = entertainers.entertainerid 
                  INNER JOIN entertainer_styles 
                  ON         entertainers.entertainerid = entertainer_styles.entertainerid 
                  INNER JOIN musical_styles 
                  ON         entertainer_styles.styleid = musical_styles.styleid 
                  WHERE      musical_styles.stylename = 'Country') engagedcustomers 
ON     engagedcustomers.customerid = customers.customerid

答案 1 :(得分:0)

正如评论中已经说明的那样,您的子查询有两列不会起作用,因为您使用的是IN。我还强烈建议您在查询中开始使用一些别名。这使得阅读更容易。以下是使用别名和子查询中的单个列查询的查找方式。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="hor_menu" id="menu">
  <li class="active" id="item1"><a>Menu item 1</a></li>
  <li id="item2">Menu item 2</li>
  <li id="item3">Menu item 3</li>
</ul>
<div id="div1">
  <p>Show me when menu item 1 selected</p>
</div>
<div id="div2" class="hide">
  <p>Show me when menu item 2 selected</p>
</div>
<div id="div3" class="hide">
  <p>Show me when menu item 3 selected</p>
</div>