我有一个这样的数据库:
banner | image | date
------------------------------
123_first | xxxxx | date
123_second | xxxxx | date
134_foo | xxxxx | date
134_foo | xxxxx | date
134_bar | xxxxx | date
我需要得到:
banner | image | date
-----------------------------
134_foo | xxxxx | date
134_bar | xxxxx | date
当我选择特定ID时(134)。 因此,我只需要在每个第一次出现时获取所有横幅ID。
我正在使用此PDO声明,但它不会返回任何内容:
$sth = $dbh->prepare('SELECT `banner` FROM `abanners` WHERE `banner` LIKE ? ORDER BY `date` DESC GROUP BY `banner`');
$sth->execute(array($user["id"] . "%"));
结果应为:
Array('134_foo', '134_bar');
我该如何解决问题?
答案 0 :(得分:1)
尝试
$id = $user["id"];
$sth = $dbh->prepare('SELECT `banner` FROM `abanners`
WHERE `banner` LIKE ? ORDER BY `date` DESC GROUP BY `banner`');
$sth->execute(array("$id%"));
答案 1 :(得分:0)
如果你有一个唯一的ID,你可以得到一个像这样的'banner'数组:
$sth = $dbh->prepare('SELECT `ID-hope_you_have_one`, `banner` FROM `abanners` WHERE `banner` LIKE ? ORDER BY `date` DESC GROUP BY `banner`');
$sth->execute(array($user["id"] . "%"));
$array_indexed_by_id = $sth->fetchAll(PDO::FETCH_KEY_PAIR);