我正在尝试编写一个查询,该查询将显示至少有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>
答案 0 :(得分:2)
问题是由WHERE
查询中尚未存在的名称引用(Fullname
和Titles
)引起的。
尽管SQL处于SELECT... FROM... WHERE... GROUP BY... HAVING... ORDER BY...
顺序,但实际上SELECT
会在最后进行评估,因此您的Fullname
和Titles
列尚不可用{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
很特别:您必须改为使用NULL
或IS NOT NULL
。