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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:27:59+00:00 2026-05-27T22:27:59+00:00

I have the following two tables CREATE TABLE accounts ( id INTEGER UNSIGNED NOT

  • 0

I have the following two tables

CREATE TABLE accounts (
 id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(255) NOT NULL
);

CREATE TABLE friends (
 id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
 account_id INTEGER UNSIGNED NOT NULL,
 friend_id INTEGER UNSIGNED NOT NULL,
 created_on TIMESTAMP NOT NULL
);

I established a JOIN query as such:

SELECT accounts.name, accounts.id, friends.account_id FROM accounts
RIGHT JOIN friends ON accounts.id = friends.account_id
ORDER BY accounts.name LIMIT 10;

It will look something like:

Mike Fenway | 1 | 3 |

Jim Morison | 3 | 1 |

The question is how do I display the friends name so that the result appear as such:

Mike Fenway | 1 | Jim Morison | 3 |

Jim Morison | 3 | Mike Fenway | 1 |

  • 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-27T22:28:00+00:00Added an answer on May 27, 2026 at 10:28 pm

    You need to perform a further join to get the friend data. I have used LEFT JOIN to always return the account, even if there are no friends. An INNER JOIN would return only accounts that have friends and omit those which do not.

    SELECT A.id, A.name, ACC.id `friendId`, ACC.name `friendName`
    FROM accounts A
    LEFT JOIN friends F ON A.id = F.account_id
    LEFT JOIN accounts ACC ON F.friend_id = ACC.id
    ORDER BY A.name LIMIT 10;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: CREATE TABLE `attendance_event_attendance` ( `id` int(11) NOT NULL AUTO_INCREMENT,
i have following 3 tables CREATE TABLE [dbo].[dspartner]( [dspartnerid] [bigint] IDENTITY(1,1) NOT NULL, [name]
I have the following two tables: CREATE TABLE `table1` ( `ID` int(11) NOT NULL
I have two tables: CREATE TABLE items ( root_id integer NOT NULL, id serial
I have the following table: CREATE TABLE posting ( id integer NOT NULL PRIMARY
I have the following two tables defined... CREATE TABLE [LogLevel] ( [Id] int primary
I have the following dump: CREATE TABLE `testdata`.`carer` ( `ID` bigint(20) NOT NULL auto_increment,
I built two tables with the following SQL: CREATE TABLE state( IDstate INTEGER not
Say I have two tables: create table parent ( number not null, constraint parent_pk
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date

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.