1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Joining two tables and returning fields from max(id) row

Discussion in 'MySQL' started by Landslyde, Mar 13, 2015.

  1. #1
    I'm an MySQL noob, so please bear with me. I have two tables:
    
    
    TABLE `attendees` (
    `attendeeid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `fname` varchar(20) NOT NULL,
    `lname` varchar(20) NOT NULL,
    `dojid` varchar(10) NOT NULL,
    `address1` varchar(25) NOT NULL,
    `address2` varchar(25) NOT NULL,
    `city` varchar(20) NOT NULL,
    `state` char(2) NOT NULL,
    `zipcode` varchar(5) NOT NULL,
    `phonenumber` varchar(15) NOT NULL,
    `memberid` int(10) unsigned NOT NULL,
    PRIMARY KEY (`attendeeid`),
    KEY `memberid` (`memberid`),
    CONSTRAINT `attendees_ibfk_2` FOREIGN KEY (`memberid`) REFERENCES `members` (`memberid`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    TABLE `history` (
    `historyid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `amount` float NOT NULL,
    `subsidy` char(1) NOT NULL,
    `last_payment` date NOT NULL,
    `amount_paid` float NOT NULL,
    `balance` float NOT NULL,
    `attendeeid` int(10) unsigned NOT NULL,
    `memberid` int(10) unsigned NOT NULL,
    PRIMARY KEY (`historyid`),
    KEY `attendeeid` (`attendeeid`),
    CONSTRAINT `history_ibfk_2` FOREIGN KEY (`attendeeid`) REFERENCES `attendees` (`attendeeid`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    Code (markup):
    What I'm trying to do is retrieve fields from History table using MAX(historyid) to find the latest info entered for an attendee, using the Attendee table for the name, join the two tables with attendeeid. My query looks like this:
    
    
    select *
    select a.attendeeid,max(h.historyid),a.fname,a.lname,h.last_payment,h.balance
    history JOIN attendees AS a
    ON a.attendeeid = h.attendeeid
    group by a.attendeeid) as maxHistoryPerAttendee
    where attendeeid = 29
    
    Code (markup):
    The problem is that, while it sure 'nuff gets the MAX(historyid) right, the row it pulls from is the very first row entered in History for that attendee. I need last_payment and balance to be associated with the MAX(historyid) row, not the row with lowest historyid. Like I said, I'm new to MySQL. I've been online all day, reading this, reading that, and I've probably seen what I need and just didn't know it. Just need a little help here, guys. Thank you.
     
    Landslyde, Mar 13, 2015 IP
  2. O-D-T

    O-D-T Member

    Messages:
    180
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #2
    how about something like

    SELECT TOP 1 *
    FROM attendees, history
    WHERE (attendees.attendeeid = history.attendeeid) AND (attendees.attendeeid = 29)
    ORDER BY history.historyid DESC
     
    O-D-T, Mar 15, 2015 IP
  3. Landslyde

    Landslyde Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I'll give that a whirl, O-D-T. Thanks for your input.
     
    Landslyde, Mar 15, 2015 IP