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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:30:56+00:00 2026-06-01T14:30:56+00:00

I’m new to MySQL and PHP. I have two tables, one to hold all

  • 0

I’m new to MySQL and PHP. I have two tables, one to hold all the company names and the other table has only the company name below the user:

Table 1
| # | Company name |
--------------------
| 1 | Microsoft    |
| 2 | HP           |
| 3 | Asus         |
| 4 | Apple        |
| 5 | Amazon       |
| 6 | CCN          |

table 2
| # | Company name | User name |
--------------------------------
| 1 | Asus         |   x1      |
| 2 | Apple        |   x1      |
| 3 | HP           |   x2      |
| 4 | Asus         |   x2      |
| 5 | Apple        |   x2      |

I need to create a query that achieves the following. First of all the companies are shown which are associated with a specific user (say Asus and Apple for user x1). After that, the remaining companies from table 1 are shown.

For example, the result of the query I’m looking for, for user X1 will display the rows in this way:

| # | Company name |
--------------------
| 1 | Asus         |
| 2 | Apple        |
| 3 | Microsoft    |
| 4 | HP           |
| 5 | Amazon       |
| 6 | CCN          |

How can I achieve this?

  • 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-01T14:30:58+00:00Added an answer on June 1, 2026 at 2:30 pm

    It looks like you want to include all companies, but for a given user, list the companies associated with that user first. If that’s the case, you do not want to use an INNER JOIN.

    Here’s some SQL that should work. I’ve provided reasonable table and field names since you didn’t give those. I’m also assuming that you have a reasonably sane table design with no duplicate rows.

    SELECT c.company_name,
    
      CASE
        WHEN u.company_name IS NULL THEN 'N'
        ELSE 'Y'
      END AS user_has_company
    
    FROM companies c
    
    LEFT JOIN (
      SELECT *
      FROM users
      WHERE user_name = 'x1'
    ) u
    ON u.company_name = c.company_name
    
    ORDER BY user_has_company DESC, c.company_name
    

    This query will return an extra column – user_has_company. I’d use that to indicate whether the current user is associated with a given company, but you can ignore it if you want.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.