GET之后的ORDER BY

时间:2017-12-21 05:59:09

标签: sql

在WHERE语句的末尾,我想添加ORDER BY来对行进行排序,例如person_name等。

<?php
    $DB_pass = 'removed';
    $DB_user = 'removed';
    $DBcon = new PDO( 'mysql:host=localhost;dbname=removed', $DB_pass, $DB_user );
    $stmt = $DBcon->query("
        SELECT
            person.id AS p_id,
            person.person_name AS person_name,
            person.person_number AS person_number,
            location.id AS l_id,
            location.location_state AS location_state,
            location.location_country AS location_country
        FROM person_location
        JOIN location ON location.id = person_location.lp_lid_fk
        JOIN person on person.id = person_location.lp_pid_fk
        WHERE
            location.id = " . $_GET["view_id"]);
    foreach ($stmt as $row)
    {
        echo '<li>' . $row['person_name'] . ' from ' . $row['location_state'] . ', ' . $row['location_country'] . '</li>';}
?>

1 个答案:

答案 0 :(得分:-2)

$stmt = $DBcon->query("
    SELECT
        person.id AS p_id,
        person.person_name AS person_name,
        person.person_number AS person_number,
        location.id AS l_id,
        location.location_state AS location_state,
        location.location_country AS location_country
    FROM person_location
    JOIN location ON location.id = person_location.lp_lid_fk
    JOIN person on person.id = person_location.lp_pid_fk
    WHERE
        location.id = " . $_GET["view_id"]." ORDER BY person_name");