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

Results from multiple tables with same column names by same id using ASP from SQL

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

我有 3 tables - PC、显示器和显示器 2。 Monitor2 table 是 Monitor table 的精确克隆,包括数据。 (不要问我为什么,在我来之前完成)

从 PC 使用的列 table PC_ID、姓名(加上其他一些但与此处无关)

Monitor 和 Monitor2 中的列 tables Monitor_ID、姓名、尺寸、PC_ID、品牌、mNotes

基本上我需要提取结果来填充 table,显示哪个 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 

如果不是那样,我会得到类似 "The multi-part identifier "Monitor2.Name“无法绑定。” 或其他一些不正确的语法错误。

欢迎任何帮助

你有

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 您会被骗。