473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

foreach parent return eldest son's picture

guillermobytes
hello i have 3 tables : parent, children, childrenPictures

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 :

Expand|Select|Wrap|Line Numbers
  1. SELECT p.name AS parentName,
  2.   cp.imgUrl AS imgUrl
  3. FROM parent AS p
  4.    RIGHT JOIN children AS c ON (p.parentId = c.parentId)
  5.    RIGHT JOIN childrenPictures AS cp ON (c.pictureId = cp.pictureId))
  6. WHERE c.age IN (SELECT c.age AS age FROM children AS c GROUP BY c.parentId ORDER BY c.age DEST LIMIT 0,1)
I would like to join the subquery but don't know how to do that, anywaysI can't figure out how to solve this...

Well if anyone has a hint i'd appreciate very much

Thank you very much,

guillermobytes
Jan 14 '11 #1
6 2059
Ok so i finally wrapped my head around it!

The query would be :

Expand|Select|Wrap|Line Numbers
  1. SELECT p.name AS parentName,
  2.    cp.imgUrl AS imgUrl,
  3.    MAX(c.age) AS age
  4. FROM parent AS p
  5.    RIGHT JOIN children AS c ON (p.parentId = c.parentId)
  6.    RIGHT JOIN childrenPictures AS cp ON (c.pictureId = cp.pictureId))
  7. GROUP BY p.name
The secret was to put a max() coupled with a group by.
Every child that has the same parent is put into a group/set (GROUP BY parent name) as GROUP BY returns only one row per distinct value in column, the max() function tells which child in the group must be returned.
Well that is how i interpreted it... lol

Regards
Jan 14 '11 #2
Rabbit
12,516 Expert Mod 8TB
Does that work? I haven't tried it but it doesn't look like it would work.

Don't you have to use a subquery to get it to work?
Jan 14 '11 #3
You are right rabbit, it doesn't work!! :S
So if any one has a hint I'm still interested!
Jan 16 '11 #4
Rabbit
12,516 Expert Mod 8TB
You'll need to use a subquery, to return the correct picture based on the child ID.
Jan 16 '11 #5
what i can't figure out how to do, is to get the correct child id, so far what i can get is the correct max age and the parent name.
Jan 16 '11 #6
Well i finally found the solution

Expand|Select|Wrap|Line Numbers
  1. SELECT mx.maxAge AS maxAge, 
  2.     cc.childrenCount AS childrenCount, 
  3.     i.localUrl AS imageLocalUrl, 
  4.     p.name AS parentName
  5. FROM (SELECT parentId, 
  6.             MAX(age) AS maxAge
  7.         FROM Children 
  8.         GROUP BY parentId) AS mx
  9. INNER JOIN (SELECT parentId,
  10.             COUNT(*) AS childrenCount 
  11.            FROM children 
  12.             GROUP BY parentId) AS cc ON (mx.parentId = cc.parentId)  
  13.     INNER JOIN children AS c ON (mx.parentId = c.parentId) 
  14.     INNER JOIN parent AS p ON (mx.parentId = p.parentId) 
  15.     INNER JOIN children_image AS i ON (c.imageId = i.imageId) 
  16.     WHERE c.maxAge = mx.maxAge 
  17.     GROUP BY c.parentId
Jan 16 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Z D | last post by:
Hello, BACKGROUND: ============== I've created a Windows User Control that contains an Image Control (among other controls). The user control handles the picture resize event. Whenever the...
6
by: Martin | last post by:
Maybe I'm tired, but I can't figure out a simple way for two controls to share the size of the parent control. e.g when the parent's height becomes NewHeight the first control's new height becomes...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
7
by: David Veeneman | last post by:
This is a very simple question, but for moe reason I can't find an answer. Do I take a performance hit if a foreach statement has to evaluate an expression? For example, consider the following:...
3
by: find clausen | last post by:
Is it possible to get the width of a picture: <img src="../graphics/press/bw21.jpg" width=400 height=555 alt="" border="0"> the make a box be exactly the width of the picture: ...
10
by: Anthony | last post by:
I am using the following code to display fading pictures on a team website. Can someone show me how to modify it such that I can display the names of each employee when the picture changes? This...
6
by: webmaster1 | last post by:
Hi, Im not a Java programmer, I do a PHP programs, but now I must implement a Java script for a poicture slideshow. With this script I can change pictures from examlpe 1 to 10. But I must to...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
3
by: JFKJr | last post by:
Hello everyone, I am trying to insert a picture in an excel spreadsheet cell using Access VBA. The following excel VBA code is working perfectly fine and I am able to insert picture in "A1" cell....
1
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me i have a online shopping center that i show pictures of the my product in home page. in the InterExplorer pictures is shown correctly but in Firefox browser is shown...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.