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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:47:10+00:00 2026-06-13T18:47:10+00:00

Example schema for my question is given at SQLFiddle and as follow: CREATE TABLE

  • 0

Example schema for my question is given at SQLFiddle and as follow:

CREATE TABLE `players` (
  `player1id` int(11) NOT NULL,
  `player2id` int(11) NOT NULL,
  PRIMARY KEY (`player1id`,`player2id`)
) ENGINE=InnoDB;

INSERT INTO `players` values
(1,5),
(1,7),
(5,3),
(5,4),
(2,1);

In a online game, I want to select opponent, which could be in player player1id or player2id.

Example Input/ output needed

Input 1: Output 5, 7, 2
Input 5: Output 1, 3 ,4
& so on.

That is, required data could be in any column but I need output in single column conditionally or any other way. I heard about MySQL conditional column but could not create query to get required output. Can someone please help with required query.

Edit

Based on this link, I run following query but failed.

SELECT IF(PLAYER1ID IS 1,PLAYER2ID as opponent,PLAYER1ID as opponent) 
FROM players
WHERE PLAYER1ID = 1 OR PLAYER2ID = 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-06-13T18:47:11+00:00Added an answer on June 13, 2026 at 6:47 pm

    I think you may use case when then syntax as below:

    SELECT CASE WHEN player1id = 1 THEN player2id ELSE player1id  END
    FROM players WHERE player1id =1 OR player2id=1;
    

    Alternatively:

    SELECT CASE WHEN player1id = 1 THEN player2id WHEN player2id =1 THEN player1id  END
    FROM players WHERE player1id =1 OR player2id=1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a schema with with a name passed by variable. Example:
My question relates to identifying which table and schema a mysql command is executed
I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]
I asked this question , and someone suggested this type of schema. I'm not
Example Schema: RowID Quantity ModifiedPrice GroupPrice CustomPrice SalePrice ---------------------------------------------------------------------------- 1 5 20.00 0 15.00
The docs simply say or define custom schema (non-juggling), for example, mongoose. Please note,
We separate features in our database using schemas. For example, all Create & Maintain
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using
I got an answer to another question here: DB Schema For Chats? It's a
In my schema, I have a table Projects, and a table Tasks. Each project

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.