SQL查找已至少播放两次的电影

时间:2019-01-08 18:27:00

标签: sql

好的,当我学习的时候,我碰到了颠簸。

我有一个包含

的数据库
City(Name,State,Population)

Theatre(Theatre_id,name,City_Name)

ScreeningRoom(ScreeningRoom_Number,theatre_tid,Capacity)

Movie(Movie_id,title,lenght)

Screening(Screening_id,Movie_fid,SreeningR_number,viewers)

我需要在每个放映室中查找所有已经放映至少2次的电影。 我设法找到了每个放映厅放映的所有电影的解决方案,我的代码如下所示

select * from movie
where not exists
    (
    select * from ScreeningRoom 
    where not exists(
        select * from Screening
        where movies.movie_id=screeing.Movie_fid
        and ScreeningRoom.ScreeningRoom_Number=Screening.Screening_id
    )
    )
group by movie.title

但是我不知道如何实施至少2次...请帮助

2 个答案:

答案 0 :(得分:0)

尝试一下。我在语句中添加了一个having子句。

String TableContents="";
foreach(....){
TableContents += " <table style='width: 100%; border-color:lightgray; border-style:solid; border-top:none; border-left:none; border-right:none; border-width:0.5px;'> "
                    + "<tr>" +

                        " <td style='width: 25%;'> " +
                           " <table style='width:95%;'> " +
                                "<tr>" +
                                   " <td>" +
                                       " <asp:ImageButton ID='ImageButton1' runat='server' CssClass='ImageBtn' ImageUrl='Images/TrackingImg.PNG' /> " +
                                   " </td>" +
                               " </tr> " +
                               " <tr>"+
                                   " <td>"+
                                       " <asp:Label ID='DashboardID' runat='server'></asp:Label>"+
                                    "</td>"+
                               " </tr>"+
                           " </table>"+
                        "</td>"+

                    "    <td style='width: 30%;'>"+
                           " <table style='width:95%;'>"+
                              "  <tr>"+
                                  "  <td>"+
                                      "  <asp:Label ID='Label6' runat='server' CssClass='DashboardLabel' Text='Project Title:'></asp:Label>"+
                                  "  </td>"+
                               " </tr>"+
                               " <tr>"+
                                  "  <td>"+
                                       " <asp:Label ID='ProjectTitleTxt' runat='server'></asp:Label>"+
                                    "</td>"+
                               " </tr>"+
                               " <tr>"+
                                    "<td>"+
                                        "<asp:Label ID='Label7' runat='server' CssClass='DashboardLabel' Text='Created By:'></asp:Label>"+
                                   " </td>"+
                               " </tr>"+
                              "  <tr>"+
                                   " <td>"+
                                 "       <asp:Label ID='CreatedByTxt' runat='server'></asp:Label>"+
                                    "</td>"+
                               " </tr>"+
                               " <tr>"+
                                  "  <td>"+
                                      " <asp:Label ID='Label9' runat='server' CssClass='DashboardLabel' Text='Date Created:'></asp:Label>"+
                                   " </td>"+
                              "  </tr>"+
                               " <tr>"+
                                  "  <td>"+
                                      "  <asp:Label ID='DateTxt' runat='server'></asp:Label>"+
                                   " </td>"+
                               " </tr>"+
                           " </table>"+
                       " </td>"+

                      "  <td style='width: 35%'>"+
                            "<table style=' width:95%;'>"+
                               " <tr>"+
                                   " <td>"+
                                          " <asp:Label ID='Label8' runat='server' CssClass='DashboardLabel' Text='Project Description:'></asp:Label>"+
                                   " </td>"+
                             "   </tr>"+
                              "  <tr>"+
                                  "  <td style=' width:95%;  height:120px;'>"+
                                     "   <asp:Label ID='DescriptionTxt' runat='server'></asp:Label>"+
                                 "   </td>"+
                              "  </tr>"+
                         "   </table>"+
                        "</td>"+
                  "  </tr>"+
                "</table>";
            }
            DashboardContainer.Text = TableContents;

答案 1 :(得分:0)

您几乎接近答案。您需要分组并在存在的部分中施加约束。

而且看来您需要使用Exists而不是不存在

select * from movie
where exists
    (
    select 1 from ScreeningRoom 
    where exists(
        select 1 from Screening
        where movies.movie_id=screeing.Movie_fid
          and ScreeningRoom.ScreeningRoom_Number=Screening.Screening_id
        group by Movie_Fid
        having count(*) >= 2 
         )
    )
group by movie.title