Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically pick field to select data from
    primarykey
    data
    text
    <p>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".</p> <p>The chosenNum field is a column that gives me the option to search for a number in a particular field. </p> <p>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.</p> <p>What might my select statement look like?</p> <hr> <hr> <p>It might be a better scenario if I say I would like to do a select statement like:</p> <p>SELECT * FROM aTable WHERE (SELECT bVal FROM bTable WHERE aVal = @varField ) = 0;</p> <p>Where @varField is the value of the value in the field with the label stored in chosenNum or either field if chosenNum = "both"</p> <p>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</p> <p>Table A: Create</p> <pre><code>CREATE TABLE `test`.`aTable` ( `id` INT NOT NULL AUTO_INCREMENT , `primaryNum` INT NULL , `secondaryNum` INT NULL , `chosenNum` CHAR(12) NULL , PRIMARY KEY (`id`) ); </code></pre> <p>Table B: Create</p> <pre><code>CREATE TABLE `test`.`bTable` ( `aVal` INT NULL , `bVal` INT NULL ); </code></pre> <p>Table A: Data</p> <pre><code>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'); </code></pre> <p>Table B: Data</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload