How to print the mysql table's cell name which meet the condition while using OR statement?

Using 'OR' in mysql query I want to print the cell name of the table by which mean i get the data .

e.g

SELECT * FROM routes where stop_1='cityA' OR stop_2='CityA' OR stop_3='CityA' OR stop_1='cityB' OR stop_2='CityB' OR stop_3='CityB'

If query runs by CityA , I want to know that cityA, is which stop? stop_1,stop_2,stop_3 ?

I want to know CityA is stop_1 or stop_2 or stop_3 ?

3 answers

  • answered 2017-06-17 17:59 RïshïKêsh Kümar

    <?php
    $sql = "SELECT * FROM routes where stop_1='cityA' OR stop_2='CityA' OR stop_3='CityA' OR stop_1='cityB' OR stop_2='CityB' OR stop_3='CityB'";
    
    if ($result = mysqli_query($con, $sql))
        {
    
        // Get field information for all fields
    
        while ($fieldinfo = mysqli_fetch_field($result))
            {
            printf("Name: %s\n", $fieldinfo->name);
            printf("Table: %s\n", $fieldinfo->table);
            printf("max. Len: %d\n", $fieldinfo->max_length);
            }
    
        // Free result set
    
        mysqli_free_result($result);
        }
    
    ?>
    

  • answered 2017-06-17 17:59 scaisEdge

    You could use case when

    SELECT 
        case when stop_1='cityA' then 'stop_1' end  check_stop_1_A
      , case when stop_2='cityA' then 'stop_2' end  check_stop_2_A
      , case when stop_3='cityA' then 'stop_3' end  check_stop_3_A
      , case when stop_1='cityB' then 'stop_1' end  check_stop_1_B
      , case when stop_2='cityB' then 'stop_2' end  check_stop_2_B
      , case when stop_3='cityB' then 'stop_3' end  check_stop_3_B     
    
      FROM routes 
      where stop_1='cityA' 
        OR stop_2='CityA' 
        OR stop_3='CityA'
        OR stop_1='cityB' 
        OR stop_2='CityB' 
        OR stop_3='CityB'
    

  • answered 2017-06-17 17:59 Saddam

    I want to know the stop name?

    $sql=mysql_query("SELECT * FROM service WHERE stop1='".$dep."' OR stop2='".$dep."' OR stop3='".$dep."' OR stop4='".$dep."' OR stop5='".$dep."' OR stop6='".$dep."' OR stop7='".$dep."' OR stop8='".$dep."' OR stop9='".$dep."' OR stop10='".$dep."' OR stop11='".$dep."' OR stop12='".$dep."' OR stop13='".$dep."' OR stop14='".$dep."' OR stop15='".$dep."' OR stop1='".$des."' OR stop2='".$des."' OR stop3='".$des."' OR stop4='".$des."' OR stop5='".$des."' OR stop6='".$des."' OR stop7='".$des."' OR stop8='".$des."' OR stop9='".$des."' OR stop10='".$des."' OR stop11='".$des."' OR stop12='".$des."' OR stop13='".$des."' OR stop14='".$des."' OR stop15='".$des."' ");