SQL使用WHERE子句和GROUP BY时参数太少

时间:2016-11-27 23:12:49

标签: sql dreamweaver coldfusion-9

我正在尝试编写一个查询,该查询将显示至少有1部电影出演的演员列表。我得到了一个"太少的参数。预计2。"我把WHERE子句放入的错误。如何解决这个问题?

<cfquery name="qry1" datasource="torture"> 
    SELECT
        Actors.ActorID,
        Actors.First_Name + ' ' + Actors.Last_Name AS Fullname,
        COUNT( Title ) AS Titles
    FROM
        Actors 
        RIGHT JOIN Videos ON Actors.ActorID = Videos.ActorID
    WHERE
        Fullname <> NULL AND
        Titles > 0
    GROUP BY
        Actors.ActorID,
        Actors.First_name,
        Actors.Last_Name;
</cfquery> 
<cfform name="form" action="Page7Receiver.cfm"> 

    <cfselect name="actor" 
        query="qry1" 
        value="ActorID" 
        display="FullName" 
        required="Yes"
        multiple="No" 
        size="8"> 
    </cfselect> 

    <br><input type="Submit" value="Submit"> 
</cfform>

1 个答案:

答案 0 :(得分:2)

问题是由WHERE查询中尚未存在的名称引用(FullnameTitles)引起的。

尽管SQL处于SELECT... FROM... WHERE... GROUP BY... HAVING... ORDER BY...顺序,但实际上SELECT会在最后进行评估,因此您的FullnameTitles列尚不可用{1}}条款。

我建议像这样重新制定查询:

WHERE

如果我们将其更改为SELECT Actors.ActorId, Actors.First_Name + ' ' + Actors.Last_Name AS FullName, COALESCE( VideoInfo.VideoCount, 0 ) AS Titles FROM Actors LEFT OUTER JOIN ( SELECT ActorId, COUNT( * ) AS VideoCount FROM Videos GROUP BY ActorId ) AS VideoInfo ON Actors.ActorId = VideoInfo.ActorId WHERE COALESCE( VideoInfo.VideoCount, 0 ) > 0 AND Actors.First_Name IS NOT NULL AND Actors.Last_Name IS NOT NULL ,那么我们可以跳过INNER JOIN检查,因为Titles > 0的性质意味着INNER JOIN没有Actor然后无论如何,Videos上都没有表连接。

ActorId

注意,SELECT Actors.ActorId, Actors.First_Name + ' ' + Actors.Last_Name AS FullName, VideoInfo.VideoCount AS Titles FROM Actors INNER JOIN ( SELECT ActorId, COUNT( * ) AS VideoCount FROM Videos GROUP BY ActorId ) AS VideoInfo ON Actors.ActorId = VideoInfo.ActorId WHERE Actors.First_Name IS NOT NULL AND Actors.Last_Name IS NOT NULL 不是有效的SQL。 <> NULL很特别:您必须改为使用NULLIS NOT NULL