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 1030573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:43:19+00:00 2026-05-16T12:43:19+00:00

I have a table customer that stores a customer_id, email and reference. There is

  • 0

I have a table customer that stores a customer_id, email and reference. There is an additional table customer_data that stores a historical record of the changes made to the customer, i.e. when there’s a change made a new row is inserted.

In order to display the customer information in a table, the two tables need to be joined, however only the most recent row from customer_data should be joined to the customer table.

It gets a little more complicated in that the query is paginated, so has a limit and an offset.

How can I do this with MySQL? I think I’m wanting to put a DISTINCT in there somewhere…

The query at the minute is like this-

SELECT *, CONCAT(title,' ',forename,' ',surname) AS name
FROM customer c
INNER JOIN customer_data d on c.customer_id=d.customer_id
WHERE name LIKE '%Smith%' LIMIT 10, 20

Additionaly, am I right in thinking I can use CONCAT with LIKE in this way?

(I appreciate that INNER JOIN might be the wrong type of JOIN to use. I actually have no clue what the difference is between the different JOINs. I’m going to look into that now!)

  • 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-16T12:43:20+00:00Added an answer on May 16, 2026 at 12:43 pm

    You may want to try the following:

    SELECT    CONCAT(title, ' ', forename, ' ', surname) AS name
    FROM      customer c
    JOIN      (
                  SELECT    MAX(id) max_id, customer_id 
                  FROM      customer_data 
                  GROUP BY  customer_id
              ) c_max ON (c_max.customer_id = c.customer_id)
    JOIN      customer_data cd ON (cd.id = c_max.max_id)
    WHERE     CONCAT(title, ' ', forename, ' ', surname) LIKE '%Smith%' 
    LIMIT     10, 20;
    

    Note that a JOIN is just a synonym for INNER JOIN.

    Test case:

    CREATE TABLE customer (customer_id int);
    CREATE TABLE customer_data (
       id int, 
       customer_id int, 
       title varchar(10),
       forename varchar(10),
       surname varchar(10)
    );
    
    INSERT INTO customer VALUES (1);
    INSERT INTO customer VALUES (2);
    INSERT INTO customer VALUES (3);
    
    INSERT INTO customer_data VALUES (1, 1, 'Mr', 'Bobby', 'Smith');
    INSERT INTO customer_data VALUES (2, 1, 'Mr', 'Bob', 'Smith');
    INSERT INTO customer_data VALUES (3, 2, 'Mr', 'Jane', 'Green');
    INSERT INTO customer_data VALUES (4, 2, 'Miss', 'Jane', 'Green');
    INSERT INTO customer_data VALUES (5, 3, 'Dr', 'Jack', 'Black');
    

    Result (query without the LIMIT and WHERE):

    SELECT    CONCAT(title, ' ', forename, ' ', surname) AS name
    FROM      customer c
    JOIN      (
                  SELECT    MAX(id) max_id, customer_id 
                  FROM      customer_data 
                  GROUP BY  customer_id
              ) c_max ON (c_max.customer_id = c.customer_id)
    JOIN      customer_data cd ON (cd.id = c_max.max_id);
    
    +-----------------+
    | name            |
    +-----------------+
    | Mr Bob Smith    |
    | Miss Jane Green |
    | Dr Jack Black   |
    +-----------------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The table's schema is pretty simple. I have a child table that stores a
I have a table that stores transaction information. Each transaction is has a unique
For example, in my WCF, if I have a Customer table and an Order
I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
Using SQL Server 2008, I have a requirement that email addresses in my user
I am doing some weekend coding exercises. I have a table that contains some
I have a script that appends some rows to a table. One of the
I have two tables Group and Customer and of course two entities Group and
This is the constraint I have on the Customers table. ALTER TABLE Customers ADD
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I

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.