MySQL SELECT MAX 多个表:foreach 父返回长子的照片

发布于 2024-10-12 12:39:04 字数 583 浏览 2 评论 0原文

**Table parent**
parentId | name

**Table children**
childId | parentId | pictureId | age

**Table childrenPictures**
pictureId | imgUrl

不,我想返回所有父母姓名及其大儿子的照片(仅返回有孩子的父母,并且只考虑有照片的孩子),

所以我想到了类似的东西:

SELECT c.childId AS childId,
   p.name AS parentName,
   cp.imgUrl AS imgUrl,
   MAX(c.age) AS age
FROM parent AS p
   RIGHT JOIN children AS c ON (p.parentId = c.parentId)
   RIGHT JOIN childrenPictures AS cp ON (c.pictureId = cp.pictureId))
GROUP BY p.name

此查询将返回每个父母的大儿子的年龄,但 childId不会对应到长子的id,因此输出不会显示正确的儿子图片。

**Table parent**
parentId | name

**Table children**
childId | parentId | pictureId | age

**Table childrenPictures**
pictureId | imgUrl

no i would like to return all parent names with their eldest son's picture (only return parents that have children, and only consider children that have pictures)

so i thought of something like :

SELECT c.childId AS childId,
   p.name AS parentName,
   cp.imgUrl AS imgUrl,
   MAX(c.age) AS age
FROM parent AS p
   RIGHT JOIN children AS c ON (p.parentId = c.parentId)
   RIGHT JOIN childrenPictures AS cp ON (c.pictureId = cp.pictureId))
GROUP BY p.name

This query will return each parent's eldest son's age, but the childId will not correspond to the eldest sons id, so the output does not show the right sons picture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-10-19 12:39:04

您需要预先查询每个家庭以获取具有图像的最大孩子的年龄,然后获取父母的姓名/图像信息...类似...

select
      PreQuery.parentID,
      p.Name ParentsName,
      c.childID,
      c.age,
      cp.imgUrl
   from 
      ( select 
              c.parentID,
              max(c.age) OldestChildAgeWithPicture
           from
              Children c,
              childrenPictures cp
           where
              c.pictureID = cp.pictureID
           group by 
              c.parentID ) PreQuery,
      Parent p,
      Children c,
      ChildrenPictures cp
   where
          PreQuery.parentID = p.parentID
      and PreQuery.parentID = c.parentID
      and PreQuery.OldestChildAgeWithPicture = c.age
      and c.pictureID = cp.pictureID

内部查询仅由父母、孩子的最高年龄进行预审图像存在...因此,如果您有一个有 3 个孩子的家庭,最大的孩子没有照片,第二大的孩子有,第二大的孩子将在上面的查询中显示。此外,如果一个家庭有双胞胎,并且两人都有照片存档,您会怎么做?如果年龄基于实际整数表示,则此查询将显示两个孩子。但是,如果年龄是根据日期算术计算的,会更准确...例如:一个孩子出生于 X 年 1 月 1 日,同一年内,另一个孩子出生于 12 月 1 日...截至 12 月,他们会年龄相同,但一个明显比另一个大 11 个月。

此外,如果您想要最古老的图片,无论是否有图片,上述查询都只需稍作更改...让我知道这是否是您正在寻找的。

you need to pre-query per family to get the age of the oldest child that has an image, then get parent's name / image information... something like...

select
      PreQuery.parentID,
      p.Name ParentsName,
      c.childID,
      c.age,
      cp.imgUrl
   from 
      ( select 
              c.parentID,
              max(c.age) OldestChildAgeWithPicture
           from
              Children c,
              childrenPictures cp
           where
              c.pictureID = cp.pictureID
           group by 
              c.parentID ) PreQuery,
      Parent p,
      Children c,
      ChildrenPictures cp
   where
          PreQuery.parentID = p.parentID
      and PreQuery.parentID = c.parentID
      and PreQuery.OldestChildAgeWithPicture = c.age
      and c.pictureID = cp.pictureID

The inner query is prequalifying just by parent, childs highest age that an image exists... So, if you have a family with 3 children, oldest has no picture, 2nd oldes DOES, the the 2nd oldest is the one that will show in the above query. Additionally, what do you want to do if a family has twins, and BOTH have pictures on file. This query would show BOTH children if age is based on actual integer representation. However, if age is computed based on date arithmetic and would be more accurate... Ex: one child born Jan 1 of year X, and within the same year, another child born Dec 1.... As of December, they would be the same integer based age, but one obviously 11 months older than the other.

Additionally, if you want the oldest REGARDLESS of having a picture, the above query would have to be changed only slightly... let me know if this is what you are looking for.

独夜无伴 2024-10-19 12:39:04

不确定,但有几点注意事项:
- 由于您只想返回带有匹配孩子的父母,因此您可以加入。
- 您可以尝试按 c.parentID 分组而不是按 p.name 分组吗?

Not sure but a couple of notes:
- Since you only want to return parents with matching children you can do just join.
- Could you try group by c.parentID instead of group by p.name?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文