Jump to content

Query Help


computermax2328

Recommended Posts

Hello all,

 

I am working on this query to show past records of data for all of the records in this particular table. This is an archive table so there are multiple entries of the same person in the database. The difference is the date that they were entered, which is an auto entry value added on the date it is uploaded. Here is the query:

 

 

"SELECT politicians.id, politicians.`Last Name`, politicians.`First Name`, politicians.`District`, archive.`firstname`, archive.`lastname`, archive.rank, archive.era FROM politicians, archive WHERE politicians.`Last Name`=archive.lastname AND politicians.`First Name`=archive.firstname ORDER BY date LIMIT 150";

 

The problem is that I only need the latest entry from the persons information and I am getting multiple from past dates. They need to be ordered by their past rank, but from the last date of the entry.

 

Any ideas??

 

P.S I know I already posted this in the applications section. I posted it there by accident

Link to comment
Share on other sites

Hey,

 

So I am coming back because I tried the subquery and it did not work.

 

To go over the details of my problem again, this is what is going on. I have an archive database with multiple entries of the same value in the database, Example:

 

Joe 1

Joe 2

Sarah 1

Sarah 3

 

The entries are dated by the date that they were entered. This what I have found out so far. I used SELECT DISTINCT in order to eliminate any other names. Query follows:

 

$sql = "SELECT DISTINCT firstname, lastname FROM archive ORDER BY rank";

 

So now I am getting all of the name indivually, just Joe and just Sarah, but what I need is for Joe and Sarah to have the ability to have the same number next to their name, so Distinct name but not number. For example:

 

Joe 1

Sarah 1

John 2

Phil 3

Bob 3

 

Any ideas?

Link to comment
Share on other sites

I did subqueires like:

 

$sql="SELECT firstname, lastname, rank FROM archive WHERE firstname=(SELECT 'First Name' FROM politicians  AND lastname=(SELECT 'Last Name' FROM politicians) ORDER BY rank";

 

I do have to get information from the politicians database as well but right now I am just working on getting the information from this one database. Again I need information like this.

 

Joe 1

Sarah 1

Steve 2

Phil 3

Alex 3

Link to comment
Share on other sites

The first thing to do is to find the latest archive date for each name.

 

SELECT firstname, lastname, MAX(date) as date
FROM archive
GROUP BY firstname, lastname

 

We will use the above as a table subquery. This behaves as though you have a table containing three columns (firstname, lastname, latestdate) so when we INNER JOIN it with the archive table we only get those archive records where these three fields match, ie the lates archive record for each name.

 

As we are using this subquery as a logical table we have to give it an alias, in this case "latest"

 

So our query becomes

 

SELECT politicians.id, politicians.`Last Name`,
   politicians.`First Name`, politicians.`District`,
   archive.`firstname`, archive.`lastname`,
   archive.rank, archive.era
FROM politicians
   INNER JOIN archive
    ON politicians.`Last Name`=archive.lastname
	    AND politicians.`First Name`=archive.firstname
   INNER JOIN
    (
    SELECT firstname, lastname, MAX(date) as date
    FROM archive
    GROUP BY firstname, lastname
    ) as latest
    ON archive.firstname=latest.firstname
	    AND archive.lastname=latest.lastname
	    AND archive.date=latest.date	    
ORDER BY date LIMIT 150;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.