简体   繁体   中英

mysql select statement to return data that isn't always in the same column.

I have a Db that contains names and outcomes of a race. I am trying to write a sql statement that will determine all a particular names outcomes. the problem is that, depending on where the race took place, local or on the road, the outcome is in a different column. for instance

    ____________________________________________________________________
    | home Runner | Road runner| road Outcome | home Outcome |   ID     |
    --------------------------------------------------------------------
    |    Jerry    |    Brian   |     323      |     350      |    1     |
    --------------------------------------------------------------------
    |   Brian     |    Jerry   |     259      |     265      |    2     |

as you can see, if I want to pull the outcome for Brian, it becomes difficult because his outcome can switch between road and home columns. I thought I could store an array with all the id's that match brians, then somehow return the outcome depending on the column brian is located in (home, road) so far I have done the following, but get stuck done towards the bottom

    $getRunner = "select id from $db where homeRunner = brian union select id from $db where roadRunner = Brian" order by id asc;
    $result=mysql_query($getRunner);
    $id=array(); // this is to hold all the Id's that match
    while($row = mysql_fetch_array($result)){
        $id[]=$row['id'];
    }
    foreach ($id as $element){
        $getOutcome = "select roadOutcome from $db where roadRunner = brian and id =$element"; 
        $result2 = mysql_query($getOutcome);
        while($row2 = mysql_fetch_array($result2)){
            echo $row2['roadOutcome'];
            echo '<br />';
        }

lots of issues here, I know. I would like to consolidate the queries if possible, not to mention make it work. How can I say something like "if homeRunner = brian then select homeOutcome, else select roadOutcome where id = $element" ?

Ultimately, I am trying to return the answer in an array so that I can graph it. I am writing this in PHP. Thanks for your help!

SELECT 
  CASE 
    WHEN HomeRunner = 'Brian' THEN 'Home'
    ELSE 'Road'
  END OutcomeType,
  CASE 
    WHEN HomeRunner = 'Brian' THEN HomeOutcome
    ELSE RoadOutcome 
  END Outcome
FROM $db
WHERE HomeRunner = 'Brian' OR RoadRunner = 'Brian'

You can achive this in a single query as:

 SELECT IF(homeRunner = 'Brian', homeOutcome, road_Outcum) AS outcum
 FROM table_name
 WHERE (homeRunner = 'Brian' OR roadRunner = 'Brian');

Try this one:

$getOutcome = "SELECT CASE 'BRIAN' 
               WHEN homeRunner THEN homeOutcome
               WHEN roadRunner THEN roadOutcome
               END FROM $db WHERE id =$element";
$result2 = mysql_query($getOutcome);
$outcome = mysql_fetch_row($getOutcome);
echo $outcome[0]; // Final Output!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM