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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:31:55+00:00 2026-05-31T03:31:55+00:00

I am basically having the exact same problem as here: SQL View: Join tables

  • 0

I am basically having the exact same problem as here:

SQL View: Join tables without causing the data to duplicate on every row?

Except on that question he was using SQL, and I am using mysql. I am wondering if the same query is possible in mysql. If so, I may have the wrong syntax?

I am trying to do something like

select a.name as account_Name, 
   p.description as property_DESCRIPTION, 
   p.address as property_ADDRESS, 
   null as vehicles_DESCRIPTION,
   null as vehicles_MAKE, 
   null as vehicles_MODEL
from Accounts a
    inner join Properties p
        on a.accountid = p.accountid
UNION ALL   
select a.name as account_Name, 
   null as property_DESCRIPTION, 
   null as property_ADDRESS, 
   v.description as vehicles_DESCRIPTION,
   v.make as vehicles_MAKE, 
   v.model as vehicles_MODEL
from Accounts a
    inner join vehicles v
        on a.accountid = v.accountid

Here is my actual code:

SELECT user.first_name, user.last_name, upi.image_id, NULL AS friends.friend_user_id FROM user 
INNER JOIN user_profile_images as upi ON (user.user_id = upi.user_id) 
UNION 
SELECT user.first_name, user.last_name, NULL AS upi.image_id, friends.friend_user_id FROM user 
INNER JOIN friends ON (user.user_id = friends.user_id) 
WHERE user.user_id = '$profile_id'

where I have 3 tables: user, user_profile_images, and friends. Both user_profile_images and friends are related to the user through the user_id. So a user can have multiple profile images as well as multiple friend entries. I can post the table diagrams if it doesnt make sense. But what I want is basically a view of all the info, with fields NULL if they don’t apply to the overall view.

If I do the query with 2 tables, either with user and user_profile_images, or user and friends, I get the desired results, but adding the third table gives me duplicate rows.

  • 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-31T03:31:57+00:00Added an answer on May 31, 2026 at 3:31 am

    The solution, as @MarcB suggests, is to use UNION rather than UNION ALL.

    However, I have a question for you – why use the UNION at all? The following is equivalent, except that if (say) account 1 has one property and one vehicle, instead of getting:

    account_Name   property_DESCRIPTION   vehicles_MAKE
      account1          property1             NULL
      account1           NULL                vehicle1
    

    You’ll get

    account_Name   property_DESCRIPTION   vehicles_MAKE
      account1          property1            vehicle1
    

    Query:

    SELECT a.name as account_Name, 
       p.description as property_DESCRIPTION, 
       p.address as property_ADDRESS, 
       v.description as vehicles_DESCRIPTION,
       v.make as vehicles_MAKE, 
       v.model as vehicles_MODEL
    FROM Accounts a
    LEFT JOIN Properties p
            on a.accountid = p.accountid
    LEFT JOIN vehicles v
            on a.accountid = v.accountid
    WHERE p.description IS NOT NULL AND v.make IS NOT NULL
    

    Note – the last line (IS NOT NULL for both p and v) simulates the ‘accounts table’ part of the INNER JOIN and makes sure that only accounts with at least a property OR a vehicle are shown. Substitute the id columns of p and v there.

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

Sidebar

Related Questions

I am having a problem with my sql query in c#, basically it's inline
Having a problem deploying an MVC application. Basically the site loads correctly, the home
Basically, I would like to be able to build a custom extractor without having
I am having a problem with binding values in my ActionScript components. I basically
I have problem that how to change text inside the progressdialog (basically having STYLE_HORIZONTAL
I'm writing an Android Sync Adapter and basically having a problem with it syncing
I have read the various questions/answers here that basically indicate that having a clustered
Basically having some trouble with using Hover to hide or show an item. The
I was looking at this question. Basically having a leading zero causes the number
Basically i am having problems with my code - this is homework so would

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.