最初我的问题是我的帖子是重复的。我相信这是因为我没有在SQL语句中链接我的表。现在我已经这样做了,我在if if.eff声明后打印出“No Data Found”。
我确信手动检查数据库中有信息,但没有成功。
这是我的代码:
<% option explicit %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="normalize.css">
</head>
<body>
<!--#include file="header.asp"-->
<!--#include file="dbconn.asp"-->
<%
dim sql, info
' 0 1 2 3 4 5
SQL = "select projectName, UserName, Category, Created, url, Projectstable.description "&_
"from ProjectsTable, PictureTable, Usertable, CategoryTable "&_
"where Projectstable.ID=PictureTable.projectNum AND "&_
"categoryNum=CategoryTable.ID AND "&_
"ProjectsTable.userNum=Usertable.ID AND "&_
"UserTable.ID=PictureTable.userNum "&_
"order by Created"
set info=conn.execute(SQL)
if info.eof then
response.write "<p>No Data Found</p>"
else
do
response.write "<h1>" & info(0) & "</h1> By: " & info(1) & " Posted in: " & info(2) & " Created on: " & info(3) & "<br>" &_
"<img src=""images/" &info(4)&""">" &_
"<p>" & info(5) & "</p>"
info.movenext
loop until info.eof
end if
conn.close
%>
</body>
</html>
答案 0 :(得分:1)
我使用ANSI 92显式连接语法而不是您使用的ANSI 89版本重写了您的查询(正如名称建议在20多年前使用显式连接替换):
SELECT projectName, UserName, Category, Created, url, Projectstable.description
FROM ProjectsTable
INNER JOIN PictureTable
ON Projectstable.ID = PictureTable.projectNum
INNER JOIN Usertable
ON UserTable.ID = PictureTable.userNum
AND ProjectsTable.userNum = Usertable.ID
INNER JOIN CategoryTable
ON categoryNum = CategoryTable.ID
ORDER BY Created;
我向您突出显示,您将结果限制在图片表中的用户与项目表中的用户相同的位置。这是故意的吗?
如果删除其中一个条件,是否会获得所需的结果?所以要么
SELECT projectName, UserName, Category, Created, url, Projectstable.description
FROM ProjectsTable
INNER JOIN PictureTable
ON Projectstable.ID = PictureTable.projectNum
INNER JOIN Usertable
ON UserTable.ID = PictureTable.userNum
--AND ProjectsTable.userNum = Usertable.ID
INNER JOIN CategoryTable
ON categoryNum = CategoryTable.ID
ORDER BY Created;
或者
SELECT projectName, UserName, Category, Created, url, Projectstable.description
FROM ProjectsTable
INNER JOIN PictureTable
ON Projectstable.ID = PictureTable.projectNum
INNER JOIN Usertable
ON ProjectsTable.userNum = Usertable.ID
--AND UserTable.ID = PictureTable.userNum
INNER JOIN CategoryTable
ON categoryNum = CategoryTable.ID
ORDER BY Created;
有关ANSI 89与ANSI 92的进一步阅读,加入语法Aaron Bertrand已经写出了a great article about it的坏习惯,可以踢出系列文章。
答案 1 :(得分:1)
答案实际上是数据库本身的错误。更明确地说,表之间的链接是不正确的。 SQL语句可能已过时但未导致错误。
答案 2 :(得分:-1)
为什么代码中存在下划线?或者第一个“'” 清理你的代码我得到:
<% option explicit %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="normalize.css">
</head>
<body>
<!--#include file="header.asp"-->
<!--#include file="dbconn.asp"-->
<%
dim sql, info
SQL = "select projectName, UserName, Category, Created, url, Projectstable.description "&
"from ProjectsTable, PictureTable, Usertable, CategoryTable "&
"where Projectstable.ID=PictureTable.projectNum AND "&
"categoryNum=CategoryTable.ID AND "&
"ProjectsTable.userNum=Usertable.ID AND "&
"UserTable.ID=PictureTable.userNum "&
"order by Created"
set info=conn.execute(SQL)
if info.eof then
response.write "<p>No Data Found</p>"
else
do
response.write "<h1>" & info(0) & "</h1> By: " & info(1) & " Posted in: " & info(2) & " Created on: " & info(3) & "<br>" &
"<img src=""images/" &info(4)&""">" &
"<p>" & info(5) & "</p>"
info.movenext
loop until info.eof
end if
conn.close
%>
</body>
</html>