Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL/PHP Selezionare solo valori univoci da più colonne e inserirli in array separati

Se vuoi mantenerlo come un'unica istruzione SQL, puoi:

$sql = "
    SELECT DISTINCT 'make' as descr,make as val FROM items
    UNION
    SELECT DISTINCT 'model' as descr,model as val FROM items
    UNION
    SELECT DISTINCT 'year' as descr,year as val FROM items
    UNION
    SELECT DISTINCT 'month' as descr,month as val FROM items
    UNION
    SELECT DISTINCT 'day' as descr,day as val FROM items
    UNION
    SELECT DISTINCT 'hour' as descr,hour as val FROM items";

$result = @mysql_query($sql, $con) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
    $make_array[$row['descr']][]=$row['val'];
}