A simple database query can grab all results. Loop through them to determine whether they belong in column A, or B, or both.
<?php
// Query all entries from database
$rs = mysql_query('SELECT Description, A, B FROM table ORDER BY Description ASC');
// Loop through all rows in result set
while ( $row = mysql_fetch_assoc( $rs ) ){
// if Column A = 1, add to $colA array
if ( $row['A'] > 0 ) $colA[] = $row;
// if Column B = 1, add to $colB array
if ( $row['B'] > 0 ) $colB[] = $row;
}
Now you need to do something with the data. Hard to tell from your question if you want it displayed in HTML format, or dumped into CSV format, or something else.
But, you now have array $colA that contains rows matching A=1, and array $colB that contains rows matching B=1.
Here's an approach to output in HTML:
<?php
// Setup the base table
$table_html = '<table><tr><th>A</th><th>B</th></tr>';
$x=0;
// Determine which array has more children
if ( count( $colA ) >= count( $colB ) ) {
$use = 'colA'; // A has more
$counter = 'colB';
} else {
$use = 'colB'; // B has more
$counter = 'colA';
}
// The variable variable lets us use either $colA or $colB, as determined by the value in $use.
// More info here: http://php.net/manual/en/language.variables.variable.php
foreach ( $$use as $row ){
// Append the current row, and if its counterpart exists, the other row
if ( $use == 'colA' ){
$table_html .= '<tr><td>' . $row['Description'] . '</td><td>' . ( isset( ${$counter}[ $x ] ) ? ${$counter}[ $x ]['Description'] : '' ) . '</td></tr>';
} else {
$table_html .= '<tr><td>' . ( isset( ${$counter}[ $x ] ) ? ${$counter}[ $x ]['Description'] : '' ) . '</td><td>' . $row['Description'] . '</td></tr>';
}
$x++; // Increment the counter
}
// Close the table
$table_html .= '</table>';
// Output to browser
echo $table_html;