好的,我放弃了,这个问题已经过去了,我正在寻求帮助。
在MySQL数据库中,我有一个类别和一个帖子表,就像标准的博客系统一样:
tblCategories (intID (PK), strTitle)
tblAreas (intID (PK), strName)
tblPosts (intID (PK), intCategoryID (FK), strTitle, htmlText)
然后我有几个博客支持表:
tblExternalLinks (intID (PK), intPostID (FK), strTitle, urlLink)
tblComments (intID (PK), intPostID (FK), strComment)
tblImages (intID (PK), intPostID (FK), urlImage)
tblLocations (intID (PK), intPostID (FK), intAreaID (FK), urlImage)
我想要一个附加到类别变量的所有帖子的列表 (让我们在伪PHP代码中说)$ intCatID,AND有一个 具有特定区域变量$ intAreaID的位置。这个SQL应该是 类似的东西:
SELECT tblPosts.* from tblPosts
INNER JOIN tblLocations ON tblLocations.intPostID=tblPosts.intID
WHERE intCategoryID=$intCatID AND intAreaID=$intAreaID GROUP BY tblPosts.intID;
如果这是正确的,有人可以告诉我吗?我不确定回合 GROUP BY部分。
我想要相同的列表,但这一次与其他所有的Counts相同 附加记录在支持表中:
SELECT tblPosts.*, intExternalLinksCount, intCommentsCount, intImagesCount, intLocationsCount ...
我的第一个猜测是使用嵌套表检索计数,或者 连接,这可能会使查询非常大。我也好奇 什么是更快的性能。这些东西是我爆炸的地方 头。
非常感谢答案......
答案 0 :(得分:0)
只是因为我很想收到批评意见,我想出了这个(非常非常难看)的问题:
SELECT
tblPosts.*,
tblTempLocations.intLocationsCount,
tblTempLinks.intLinksCount,
tblTempImages.intImagesCount,
tblTempComments.intCommentsCount,
tblTempNearest.fltNearestLocationAngle
FROM tblAdverts
LEFT JOIN
(SELECT intPostID, count(*) as intLocationsCount
FROM tblLocations
GROUP BY intPostID)
tblTempLocations ON tblPosts.intID=tblTempLocations.intPostID
LEFT JOIN
(SELECT intPostID, count(*) as intLinksCount
FROM tblLinks
GROUP BY intPostID)
tblTempLinks ON tblPosts.intID=tblTempLinks.intPostID
LEFT JOIN
(SELECT intPostID, count(*) as intImagesCount
FROM tblImages
GROUP BY intPostID)
tblTempImages ON tblPosts.intID=tblTempImages.intPostID
LEFT JOIN
(SELECT intPostID, count(*) as intReviewsCount
FROM tblComments
GROUP BY intPostID)
tblTempComments ON tblPosts.intID=tblTempComments.intPostID
LEFT JOIN
(SELECT intPostID,
MIN(SQRT(POWER((fltLatitude - $fltCurrentLatitude), 2) + POWER((fltLongitude - $fltCurrentLongitude),2))) AS fltNearestLocationAngle
FROM tblLocations
GROUP BY intPostID)
tblTempNearest ON tblPosts.intID=tblTempNearest.intPostID
INNER JOIN tblLocations ON tblPosts.intID=tblLocations.intPostID
WHERE tblLocations.intLocationID=$intAreaID AND tblPosts.strTitle LIKE '%$strPartialTitle%' AND tblAdverts.intCategoryID=$intCatID
GROUP BY tblAdverts.intID