I have a list of columns of table that holds boolean values. consider a,b,c,d,e,f,g,h as column names with YES or NO values. I want to query and list column names whose value is YES. can some write a mysql query that can list the columns. if a column has an underscore i want to replace underscore with space or " ".can somebody write the required script using php.

Thanks for help.

Recommended Answers

All 5 Replies

Select * FROM Replace_this_with_the_table_name_where_those_columns_are WHERE names = 'YES'

Try it if it will help you. Good Day!!

I mean this: suppose you fetch and get for instance a=NO,b=NO,c=YES,d=NO,e=YES,f=YES,g=NO,h=YES. filter the array picking out variables with value YES from the arry. List them and list those variables with value NO. for the above query, I wanted a query that can list clolumns of a particular row with value YES. I lsit them and list others later.

@mogaka, sorry but we don't just write code for you, we help you to write your own, with help and direction, and a link to relevent manuals where needed.

you are basically asking for a method to perform conditional column selection based on the column value. This is not something that can be done within a pure MySQL statement to the best of my knowledge. My suggestion would be to run the mysql statement to selecting all columns that could have a positive value and then itterate through them based on thier value using a php while or foreach loop. something to this effect

$entry=mysql_fetch_assoc(mysql_query("SELECT a,b,c,d,e,f,g FROM table WHERE ..."));
foreach ($entry as $key => $value){
    if($value=='YES'){
        $clean_entry[str_replace('_', ' ', $key)]=$value;
    }
}
print_r($clean_entry);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.