gpt4 book ai didi

mysql - 经典 ASP - 我可以丢失我的嵌套循环以换取连接吗?

转载 作者:行者123 更新时间:2023-11-29 01:44:41 25 4
gpt4 key购买 nike

我正在尝试优化其中一个脚本。它适用于图片库网站,并使用 WHILE WEND 循环显示大量图像网格,循环遍历我的数据库 (MySQL) 照片表。

在这个循环中,我有另一个 WHILE END 循环,它从我的人物表中获取每张照片中人物的名字。

我的问题是,我可以通过向我的主要查询添加 JOIN 来失去第二个 WHILE WEND 循环吗?

我的困惑来自返回不止一个人。我不知道如何得到一个或多个用逗号分隔的名字,我只是不知道。也许我的脚本就这样很好,但我确信如果没有嵌套循环它会更快,因为在某些时候,我正在考虑添加另一个查询,它返回每张照片的前 5 个关键字,并且不要不想添加另一个循环!

屏幕截图

enter image description here

我的伪代码

Set rsPhotos = Conn.Execute(" " _
& "SELECT photoID, setID, caption, dateCreated, dateUploaded " _
& "FROM photos ORDER BY dateCreated DESC;)"

While NOT rsPhotos.EOF
Response.Write "<div>"
Response.Write "<img src=""photo.jpg"">"
Response.Write "<p>"
people_str = ""
Set rsPeople = Conn.Execute("SELECT p.person FROM photoPeople AS pp " _
& "LEFT JOIN people AS p USING (personID) " _
& "WHERE pp.photoID = "&rsPhotos.Fields("photoID")&" " _
& "ORDER BY p.person ASC;")
While NOT rsPeople.EOF
people_str = people_str & ", " & rsPeople.Fields("person")
rsPeople.MoveNext
Wend
rsPeople.Close
Response.Write Mid(people_str,3)
Response.Write "</p>"
Response.Write "<div>"
rsPhotos.MoveNext
Wend

rsPhotos.Close

数据库模式

PHOTOS tbl
photoID | INT 9
setID | INT 9
caption | VARCHAR2000
dateCreated | DATETIME
dateUploaded | DATETIME

PHOTOPEOPLE tbl
photoID | INT 9
personID | INT 7

PEOPLE tbl
personID | INT 7
person | VARCHAR 100

最佳答案

这应该可以做到 -

SELECT photos.photoID, photos.setID, photos.caption, photos.dateCreated, photos.dateUploaded, GROUP_CONCAT(p.person ORDER BY p.person ASC SEPARATOR ', ')
FROM photos
LEFT JOIN photoPeople AS pp
ON photos.photoID = pp.photoID
LEFT JOIN people AS p
ON pp.personID = p.personID
GROUP BY photos.photoID
ORDER BY dateCreated DESC

GROUP_CONCAT()

关于mysql - 经典 ASP - 我可以丢失我的嵌套循环以换取连接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com