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

  • SEARCH
  • Home
  • 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 7829257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:41:44+00:00 2026-06-02T10:41:44+00:00

I have two tables containing peoples data, first name, last name etc. The first

  • 0

I have two tables containing peoples data, first name, last name etc.

The first table contains the data I want to select if it matches the stuff from the second table. The problem is the first table might have only an initial for the first name.

I have tried variations of the following query but I am missing something obviously. I don’t mind if “J Bloggs” and “Joe Bloggs” from table 1 both match “Joe Bloggs” from table 2 that’s fine.

SELECT t1.* ,  LEFT(t2.FIRST_NAME, 1) AS firstChar 
FROM t1, t2
WHERE t1.surname = t2.LAST_NAME
AND t1.firstname  = t2.FIRST_NAME
OR t1.firstname  = t2.firstChar
GROUP BY t1.surname
  • 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-06-02T10:41:46+00:00Added an answer on June 2, 2026 at 10:41 am

    You need to put () around your second WHERE condition as in:

    SELECT t1.* ,  LEFT(t2.FIRST_NAME, 1) AS firstChar 
    FROM t1, t2
    WHERE 
      t1.surname = t2.LAST_NAME
      AND (t1.firstname  = t2.FIRST_NAME OR t1.firstname  = LEFT(t2.FIRST_NAME, 1)
    ORDER BY t1.surname
    

    However, the implicit join syntax is discouraged and deprecated. Instead, use

    SELECT t1.* ,  LEFT(t2.FIRST_NAME, 1) AS firstChar 
    FROM 
      t1 JOIN t2 
        ON t1.surname = t2.LAST_NAME
          AND (t1.firstname  = t2.FIRST_NAME OR t1.firstname  = LEFT(t2.FIRST_NAME, 1)
    ORDER BY t1.surname
    

    Since your condition is that the last name must match, and the first name must either match fully or match the first character, the first name conditions have to be grouped inside ().

    I have also replaced your GROUP BY with ORDER BY, since you are not performing any group aggregate functions (SUM(), COUNT(), AVG()). MySQL doesn’t permit aliases in the WHERE clause, so I have replaced t2.firstChar with LEFT(t2.firstChar, 1) as well.

    Finally, you may find a need to convert the case of all the names with UPPER() or LOWER() on both sides of the join to be sure it matches where casing may be inconsistent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables set up. The first table contains a listing of apartments,
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables USER and TRANSACTION i want to choose the data from
I have two tables: Table1: id attr1 fk1(Table2) fk2(Table2) Table2: id name I want
I have two tables. Table A: Contains a list of songs, song artwork, mp3
I have two tables containing data that are essentially headline lists from different sources.
I have two tables, one containing a list of different options users can select
I have two tables table 1 : id, name, age, gender. table 2(friend) :
I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have two tables, one containing cities and one containing countries. Those are bi-directional

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.