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

The Archive Base Latest Questions

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

Say if I have 2 tables. The first one holds users ids and their

  • 0

Say if I have 2 tables. The first one holds users ids and their first names. The second one holds user ids and their last names, but the rows in this table may or may not exist depending on whether the user has given their last name or not.

I want to select both the first name and the last name, but if only the first name exists then to just select that on its own.

I cant use something like this because if the second table row doesn’t exist then it returns nothing:

$db->query("select firstname.fname, lastname.lname from firstname, lastname where firstname.userid = lastname.userid");

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-28T07:38:10+00:00Added an answer on May 28, 2026 at 7:38 am
    SELECT f.fname, l.lname
    FROM firstname f
       LEFT JOIN lastname l
          ON f.userid = l.userid
    

    this will return something like:

    fname | lname
    John  | Doe
    Bob   | NULL
    

    where NULL means that Bob hasn’t got a last name

    JOIN is more performant than cartesian product you are using in your example because it won’t produce all the possible combinations of {firstame,lastname} but just the ones which make sense (the ones with the same userid)

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

Sidebar

Related Questions

let's say I have three tables (MySQL), one for users, one for tags and
Say I have three tables, a table of users, a table of around 500
Say we have two tables in an MS Access db: Service Users: | ID
Say I have two tables User ----- id first_name last_name User_Prefs ----- user_id pref
Lets say I have two tables, one for transactions, and another table who's primary
I have an SQLite DB with one table that holds IDs for several strings,
Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users
Say I have three tables, A, B and C. Conceptually A (optionally) has one
First time post. As an example, say I have a table with 12 entries;
Say I have two tables called A (fields: id, phase, name) and B(fields: id,

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.