使用SQL中的ASP从具有相同列名的多个表的结果

时间:2016-02-09 16:55:10

标签: sql sql-server asp-classic

我需要一些帮助写入ASP从SQL获取数据。

我有3张桌子 - PC,Monitor&即监视器。 Monitor2表是Monitor表的精确克隆,包括数据。 (不要问我为什么,在我的时间之前完成)

PC表中使用的列 PC_ID,名称(加上其他几个但在这里不相关)

Monitor&中的列Monitor2表 Monitor_ID,名称,大小,PC_ID,制作,mNotes

基本上我需要拉取结果来填充一个表,该表显示哪个PC.Name具有哪个Monitor.Name,Monitor2.Name和其他属性。

例如

|  PC Name  |  Monitor 1  |  Monitor 2  |  Other 1 |  Other 2 |
---------------------------------------------------------------
|   PC254   |    M133     |     M176    |    27    |   27     |

这是我到目前为止所拥有的

sqlq = "SELECT PC.PC_ID, PC.PC_Name, PC.CPU, PC.RAM, PC.Make, PC.Model, PC.Date, Monitor2.Name as mon_name2, Monitor2.Size as mon_size2, Monitor.Name as mon_name, Monitor.Size as mon_size FROM Monitor right JOIN PC ON Monitor.PC_ID = PC.PC_ID FROM Monitor2 right JOIN PC ON Monitor2.PC_ID = PC.PC_ID
set rs = conn.execute(sqlq)
if rs.eof or rs.bof then
response.write("No Records Found")
else
'count how many records have been returned
count=0
rs.movefirst
pcname=""
do while not rs.eof
if pcname <> rs("PC_Name") then
count=count+1
end if
pcname=rs("PC_Name")
rs.movenext
loop

Response.write("<font size='2'>No. of records returned:" & count & " </font>")
rs.movefirst
pcname=""
%>
<%if not PrintF then %>

然后得到像

这样的输出
<%do while not rs.eof
if pcname <> rs("PC_Name") then
%>
<tr>
    <td style="width: 96px"><a href="PCView.asp?PC=<%=rs("PC_ID")%>"><font size="1"><%=rs("PC_Name")%></font></a></td>
    <td><a href="PCView.asp?PC=<%=rs("PC_ID")%>"><font size="1"><%=rs("Name")%></font></a></td>

    <td style="width: 99px"><font size="1"><%=rs("mon_name")%></font></td>
    <td style="width: 99px"><font size="1"><%=rs("mon_name2")%></font></td>
    <td><font size="1"><%=rs("mon_size")%></font></td>
    <td><font size="1"><%=rs("mon_size2")%></font></td>

</tr>
<%
end if
pcname=rs("PC_Name")
rs.movenext
loop%>

但是到目前为止我无法绕过

Microsoft OLE DB Provider for SQL Server error '80040e14' 

Incorrect syntax near the keyword 'FROM'. 

/PCs/SearchResult.asp, line 31 

如果不是那样的话我会得到类似的东西 &#34;多部分标识符&#34; Monitor2.Name&#34;无法受约束。&#34; 或其他一些不正确的语法错误。

为任何帮助干杯

1 个答案:

答案 0 :(得分:2)

你有

FROM Monitor right JOIN PC ON Monitor.PC_ID = PC.PC_ID FROM Monitor2

取出第二个FROM .. FROM Monitor2

这将离开你

SELECT  PC.PC_ID,
        PC.PC_Name,
        PC.CPU,
        PC.RAM,
        PC.Make,
        PC.Model,
        PC.Date,
        Monitor2.Name AS mon_name2,
        Monitor2.Size AS mon_size2,
        Monitor.Name AS mon_name,
        Monitor.Size AS mon_size
FROM    Monitor
        RIGHT JOIN PC ON Monitor.PC_ID = PC.PC_ID
        RIGHT JOIN PC ON Monitor2.PC_ID = PC.PC_ID

这没有意义,也是不正确的。我认为你正试图做这样的事情

SELECT  PC.PC_ID,
        PC.PC_Name,
        PC.CPU,
        PC.RAM,
        PC.Make,
        PC.Model,
        PC.Date,
        Monitor2.Name AS mon_name2,
        Monitor2.Size AS mon_size2,
        Monitor.Name AS mon_name,
        Monitor.Size AS mon_size
FROM    PC
        LEFT JOIN Monitor ON Monitor.PC_ID = PC.PC_ID
        LEFT JOIN Monitor2 ON Monitor2.PC_ID = PC.PC_ID

如果您在Monitor或Monitor2中为一个PC_ID有多个记录,您将获得欺骗。

相关问题