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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:22:14+00:00 2026-06-13T07:22:14+00:00

Say I have a table with three columns primaryNum, secondaryNum, chosenNum. primarynum and secondaryNum

  • 0

Say I have a table with three columns primaryNum, secondaryNum, chosenNum. primarynum and secondaryNum are both number values but chosenNum’s value can either be “primaryNum”, “secondaryNum”, or “both”.

The chosenNum field is a column that gives me the option to search for a number in a particular field.

For example: I might want to try to find all rows with the value 10 in the column that is stored in chosenNum. If the value of chosenNum is “both” then the row would be returned if either column (primaryNum, secondaryNum) had a value of 10.

What might my select statement look like?



It might be a better scenario if I say I would like to do a select statement like:

SELECT * FROM aTable WHERE (SELECT bVal FROM bTable WHERE aVal = @varField ) = 0;

Where @varField is the value of the value in the field with the label stored in chosenNum or either field if chosenNum = “both”

This would result in me getting back rows with id 1,2,3,4,6,7,14,15,16,19,20,21,23,24,27

Table A: Create

CREATE  TABLE `test`.`aTable` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `primaryNum` INT NULL ,
  `secondaryNum` INT NULL ,
  `chosenNum` CHAR(12) NULL ,
  PRIMARY KEY (`id`) );

Table B: Create

CREATE  TABLE `test`.`bTable` (
  `aVal` INT NULL ,
  `bVal` INT NULL );

Table A: Data

INSERT INTO test.aTable VALUES (1,8,7,'secondaryNum'),(2,2,9,'secondaryNum'),(3,7,9,'both'),(4,5,1,'both'),(5,10,3,'secondaryNum'),(6,10,6,'both'),(7,7,8,'both'),(8,10,2,'primaryNum'),(9,2,1,'secondaryNum'),(10,7,2,'secondaryNum'),(11,2,2,'secondaryNum'),(12,5,1,'secondaryNum'),(13,1,6,'primaryNum'),(14,6,6,'both'),(15,4,9,'both'),(16,9,7,'primaryNum'),(17,8,3,'secondaryNum'),(18,10,7,'primaryNum'),(19,8,5,'secondaryNum'),(20,1,7,'both'),(21,7,9,'both'),(22,8,3,'primaryNum'),(23,6,2,'primaryNum'),(24,5,7,'both'),(25,2,1,'both'),(26,5,2,'secondaryNum'),(27,7,8,'primaryNum');

Table B: Data

INSERT INTO test.bTable VALUES (1,1),(2,1),(3,1),(4,1),(5,0),(6,0),(7,0),(8,1),(9,0),(10,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-13T07:22:15+00:00Added an answer on June 13, 2026 at 7:22 am

    You can do something like this:

    select * 
    from MyTable
    where (chosenNum in ('both', 'primaryNum') and primaryNum = 10)
        or (chosenNum in ('both', 'secondaryNum') and secondaryNum = 10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table with three columns, key , value and priority
Lets say I have a database table which consists of three columns: id ,
If I have three columns in my MySQL table people , say id, name,
I have an html table with say 5 columns. Three of the columns I
Let's say I'm having a table with three columns: UserId TeamId IsTeamCaptain. How can
Ok, so lets say I have a table comprising three columns. The first column
Let's say I have a table of two columns an three rows. Now, In
Say I have a database table with three columns: ID, Name, and Age. I
Let's say I have the following table with three columns: id | start_block |
Let's say I have a table called PEOPLE having three columns, ID , LastName

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.