我有以下查询,并且只想列出第一个匹配项。
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
(?category=b)
所以 DISTINCT 可以做到这一点吗?这是我尝试过的,但没有奏效:
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
编辑:这是完整的代码:
function getCategory() {
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'") or die(mysql_error());
//$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "Geen resultaten gevonden.";
} else {
while ($row = mysql_fetch_assoc($query)) { ?>
<p><a href="/<?= $_GET['category']; ?>/<?= strtolower($row['authorclean']); ?>/"><?= $row['author']; ?></a></p>
<?php }
}
}
(B% 仅用于测试)
如果我直接在数据库中运行以下查询,我会得到两个结果。如果我使用上面的代码运行,我只会得到一个空页面(除了已经存在的 html)。
SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'