ASP检索数据库记录,然后使用第一个记录从另一个表中检索更多记录

时间:2013-09-02 18:05:20

标签: sql asp-classic

有点奇怪的。我在数据库中有一个表,用于存储另一个表中记录的ID。我需要检索并以某种方式存储table1中的信息。

信息: 产品,图像ID,数量

因此Image ID将是table2中记录的ID。我将用它来获取该记录。使用该记录,我将找到图像的保存位置,并将图像写入文档的次数与存储在该产品的数量值中的次数相同。

希望您了解我需要如何工作。这是一些示例数据。

-Product 1-
Product -> "KR"
Image ID -> 772
Quantity -> 2

-Product 2-
Product -> "KR"
Image ID -> 774
Quantity -> 5

-Product 3-
Product -> "LR"
Image ID -> 776
Quantity -> 1

现在使用上述数据,使用Image ID作为行唯一ID从table2获取数据并存储保存位置。

这就是我现在正在尝试的,但它似乎没有用。

<!--#include virtual="nightclub_photography\asp\functions.asp"-->
<%
'Connect to database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")

'Get the current date
today = ISODate(Request.Cookies("sdate") & " 00:00:00")

'Query the database for sales
sql = "SELECT * FROM sales WHERE DATEVALUE(saleDate) = " & today & " AND printed = 0 ORDER BY product"
Set rs = conn.Execute(sql)

Set sales = Server.CreateObject("Scripting.Dictionary")

if rs.EOF then
    response.write "No sales found!"
else
    do until rs.EOF
        'response.write rs("product")&":"&rs("quantity")&", "
        if sales.Exists(rs("product")) then
            sales.Item("product") = ","&rs("jpgId")&":"&rs("quantity")
        else
            sales.Add rs("product"), rs("jpgId")&":"&rs("quantity")
        end if      

        'Move to next sale
        rs.MoveNext
    loop
end if

rs.close
conn.close
%>

更新1 得到一个错误,现在简化一下,只是想先得到一些结果。

错误: Type mismatch in expression.

查询: sql = "SELECT * FROM images WHERE ID IN (SELECT jpgId FROM sales WHERE printed = 0)"

因此,使用编号为“2”的行中的jpgId。将返回具有标记为“1”的相同ID的行。它将为每个销售行执行此操作。

See image

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解,但你可能会尝试这样的事情

从mytable中选择id,*,其中id为(从mytable2中选择id,其中salesdate = date,print = 0)

更新mytable2 printed = 1,其中id =“&amp; myid