Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3432638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:28:26+00:00 2026-05-18T07:28:26+00:00

newb question there. Having MySQL database/tables structure below: CREATE DATABASE `dbTest` CREATE TABLE IF

  • 0

newb question there. Having MySQL database/tables structure below:

CREATE DATABASE `dbTest`

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbUser` (

  `UserId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

  `Username` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`UserId`) )

ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbTransactionType` (

  `TypeId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

  `Name` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`TypeId`) )

ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `dbTest`.`tbTransaction` (

  `TransactionId` INT NOT NULL AUTO_INCREMENT ,

  `UserId` INT UNSIGNED NOT NULL ,

  `TransactionType` INT UNSIGNED NOT NULL ,

  `Balance` DOUBLE NOT NULL ,

  `Date` DATETIME NOT NULL ,

  PRIMARY KEY (`TransactionId`) ,

  INDEX `FK_tbTransaction_tbUser_UserId` (`UserId` ASC) ,

  INDEX `FK_tbTransaction_tbTransactionType_TransactionId` (`TransactionType` ASC) ,

  CONSTRAINT `FK_tbTransaction_tbUser_UserId`

    FOREIGN KEY (`UserId` )

    REFERENCES `dbTest`.`tbUser` (`UserId` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `FK_tbTransaction_tbTransactionType_TransactionId`

    FOREIGN KEY (`TransactionType` )

    REFERENCES `dbTest`.`tbTransactionType` (`TypeId` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB

INSERT INTO `dbTest`.`tbUser` (`UserId`, `Username`) VALUES ('1', 'User1');
INSERT INTO `dbTest`.`tbTransactionType` (`TypeId`, `Name`) VALUES ('1', 'Deposite');
INSERT INTO `dbTest`.`tbTransactionType` (`TypeId`, `Name`) VALUES ('2', 'Withdraw');

INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '1', '200', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '2', '100', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '1', '20', NOW())
INSERT INTO `dbTest`.`tbTransaction` (`TransactionId`, `UserId`, `TransactionType`, `Balance`, `Date`) VALUES (NULL, '1', '2', '10', NOW())

I would like to get the last entry of each type of transaction for the UserId 1

This is my SQL query:

SELECT U.Username, TT.Name, T.Balance, T.Date
FROM tbUser U
INNER JOIN tbTransaction T ON T.UserId = U.UserId
INNER JOIN tbTransactionType TT ON TT.TypeId = T.TransactionType
WHERE U.UserId = 1

And the result is:

Username    Name        Balance Date
User1   Deposite    200     2010-11-26 23:11:40
User1   Deposite    20      2010-11-26 23:14:56
User1   Withdraw    100     2010-11-26 23:11:58
User1   Withdraw    10      2010-11-26 23:14:56

When I would like to get something like:

Username    Name        Balance Date
User1       Deposite    20      2010-11-26 23:14:56
User1       Withdraw    10      2010-11-26 23:14:56

I’m sure the solution isn’t that hard but I can’t figure out right now…

I’ve also tried a GROUP BY TT.TypeId with no success.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:28:26+00:00Added an answer on May 18, 2026 at 7:28 am

    I think this should do it, and it won’t be an issue if you add more transaction types. However, I haven’t quite tested bc I don’t exactly have an “isomorphic” (probably not strictly the correct term) set of tables handy to test on. Please let me know even if you’re solved, I’m curious.

    SELECT U.Username, TT.Name, T.Balance, T.Date
    FROM tbUser U
    INNER JOIN tbTransaction T ON T.UserId = U.UserId
    INNER JOIN tbTransactionType TT ON TT.TypeId = T.TransactionType
    WHERE U.UserId = 1 AND 
           T2.Date = (SELECT MAX(T2.date) 
                     FROM tbTransaction T2 WHERE 
                     T2.UserID = U.UserId, T2.TransactionType = T.TransactionType)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a ruby newb and am having a problem with procs created inside
I'm new to the Oracle Platform (having primarily used MySQL, with a little Postgres
Apologies for the terribly newb question. We're currently implementing Google Web Optimizer in our
Newb question: jQuery('.foo')[0] does the job most of the time. jQuery('.foo:first')[0] is slightly more
Super simple question from an ASP newb: I've got an internal-only ASP.NET website I'm
I'm a newb to Python so apologies in advance if my question looks trivial.
Hey my first question on SO! Anywho... Still relatively a newb at SQL so
This is probably quite a newb question, but I can't seem to figure it
Please forgive the newb question. I am very new to .NET so don't assume
Here's some question about oop in js (questions in the code below). <html> <script>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.