分组SQL查询

I am working on a movie archive management system, I need to group the query results in the cases with more than one result

For example, when a user searches a director name(e.g. Steven), There are movies of Steven A, Steven B and Steven C,(multiple) what I want to do is display all movies of Steven A under his name, Steven B under his name etc.

How can I do this with php and mysql

Thanks

Use ORDER BY director_name. This will cause rows with the same name to be listed together.

You can also use some code in PHP to detect when the director name changes add a header if you wish to make the change from one director to the next more obvious.

while (/* read row */)
{
    $director_name = ...; // Implement this.

    if ($previous_director_name !== $director_name) {
        echo ...; // Output director's name.
        $previous_director_name = $director_name;
    }

    echo ...; // Output row.
}

Since there can be people with same name (and some people actually change names), checking based on directors name would be somewhat foolish. Also, I'm gonna assume that you wanna store the information in some data-structure, instead of just echoing at the random place in your application :

$director_movies = array();

while ( /* can get new row */ )
{
    $id = $row['director_id'];
    if ( ! array_key_exists( $id, $director_movies ) )
    {
        $director_movies[ $id ] = array();
    }
    $director_movies[$id][] = $row['movie_title'];
}

// now $director_movies is a 2D array, containing movies for each director

This of course is an extremely simplified example.