• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2150
  • Last Modified:

PHP/MySql - INNER JOIN from another query

Hi!
  I would like to know how i could achieve a query with the result of another query.. here is my example:

$sql = "SELECT tblranking.IdMembre, Max(tblranking.myDate) AS MaxOfmydate FROM tblranking WHERE Type='DOUBLE' GROUP BY tblranking.IdMembre";
      $result = SQL_Query($sql);

      $sql2 = "SELECT tblranking.IdRanking, tblranking.IdMembre, tblranking.myDate, tblranking.Points FROM tblranking INNER JOIN $result ON (tblranking.IdMembre = $result["IdMembre"]) AND (tblranking.myDate = $result["MaxDemydate"]) ORDER BY Points DESC";
      $result2 = SQL_Query($sql2);

my first query get the ranking up-to-date and the second get the points each member get and i sort it to get the one who has the more points.

The problem is that i don't know how to make the INNER JOIN $result ON ... i know result is an array, but is there a way to make it work?

Oh and if you happen to know how i can test it in PhpMyAdmin, it would be awesome!

It's gonna help me alot for future joins using MySql.

Thanks in advance.
0
PUB_UL
Asked:
PUB_UL
  • 2
  • 2
1 Solution
 
temporoCommented:
I have not done much php but could you get both the answers from the database into the arrays.

then use a function with loops to sort out the rank of each of the member depending on the number of points.
0
 
temporoCommented:
here is an extract from a mysql manual:

Note that in versions before Version 3.23.17, the INNER JOIN didn't take a join_condition!

so are you using the version before that?

temporo
0
 
PUB_ULAuthor Commented:
I would prefer to know how i could do it using two queries because i'm gonna work on a stats thing and i know i'm gonna need to do similar queries...

I thought of your suggestion and it would work, but it ain't what i am looking for.

Thanks for you help, still a good suggestion :)
0
 
PUB_ULAuthor Commented:
I can do a INNER JOIN, but it's from the result of another query, not another table. That's the problem.

I could make a temporary table too using a SELECT... INTO, but i wanna know if i could make it work using the way i showed above.

Maybe it ain't possible, but if it is, i assume there is someone out there who could help me out with it.

Thanks again!
0
 
wjdashwoodCommented:
I think the inner join isn't the way to go.
The only way I can see it working is to do it is by using a select statement within a where command. So you would have something like

 $sql2 = "SELECT tblranking.IdRanking, tblranking.IdMembre, tblranking.myDate, tblranking.Points FROM tblranking
WHERE tblranking.IdMembre=(SELECT  tblranking.IdMembre FROM tblranking WHERE Type='DOUBLE' GROUP BY tblranking.IdMembre)

but that will need a fair bit of work! Its just there to give you an idea of what I mean. I think with that method you can only get one value in the where command, so you will have to get max from the original select statement.

I'm certain it is possible. It may help to write out what you want in english and then try to work out how to do it in sql.

As for the testing in phpmyadmin any straight sql can be entered and run but if you are using variables or results of other commands then you will have to substitute the value to test it.

Hope that helps a bit. I'm a bit out of touch with my sql at the moment, otherwise I would have had a go at writing the code for you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now