SQL / PHP PDO选择随机行

时间:2014-02-18 12:40:34

标签: php pdo mysqli

我是PDO的新手。我希望能够随机选择一名没有参加考试的学生('N')并回复姓名和科目。我怎样才能做到这一点?

$query = $db->prepare('SELECT name FROM exams WHERE faced = ?');
$array = array('N');
$query->execute($array);

enter image description here

1 个答案:

答案 0 :(得分:5)

您可以使用以下内容:

$query = $db->prepare('SELECT name, subject
          FROM exams WHERE faced = ?
          ORDER BY RAND() LIMIT 1');
$array = array('N');
$query->execute($array);

$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);

var_dump($result);