我在过去几天里学到了很多东西,但弄清楚如何排除重复记录让我感到很难过......
搜索" abba"使用标题搜索按钮 2结果返回
注意它只是在另一张光盘上重复。
我如何才能显示第一个找到的? 这只是一个例子:有100个重复,这是正常的。它们只是不同的光盘集。但是,Sound Choice和DKKaraoke是不同的录制标签,并不重复。因此,如果Disc ID以相同的代码开头,则仅重复 - 即DK088和DK056是一个骗局,但DK088和SC5101不是同一艺术家和标题的傻瓜......或者比较Brand可能更容易。
这是我目前的代码:
<?php
include("connect.php");
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
ini_set('session.cookie_lifetime',900);
ini_set('session.gc_maxlifetime',900);
session_start();
if(isset($_POST['searchbtn'])) { $_SESSION['searchterm']=$_POST['searchterm']; }
?>
<html>
<head>
<title>1KaraokeDJ.com Search</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<form method="post">
<p><code>
<img src="top1.jpg" /><br>
<?php if ($detect->isMobile()) { echo("Mobile Device Detected"); } else { echo("Desktop Browser Detected"); } ?>
</code></p>
<p>
<?php if(isset($_SESSION['searchterm'])) { ?>
<input name="searchterm" type="search" value="<?php echo $_SESSION['searchterm'];?>" class="search" />
<?php } else { ?>
<input name="searchterm" type="search" placeholder="Enter Search Terms Here" class="search" />
<?php } ?>
</p>
<p>
<input name="searchbtn" type="submit" id="searchbtn" class="hiddenbutton" value="Karaoke Search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Artist Search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Title Search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Disc ID Search" />
</p>
<hr style="width:100%">
<?php if(isset($_SESSION['searchterm'])) {
if(($_SESSION['searchterm']!="")) {
$searchterm=strtoupper($_SESSION['searchterm']);
switch ($_POST['searchbtn']) {
case 'Karaoke Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Artist like '%$searchterm%' || Title like '%$searchterm%' || Disc like '%$searchterm%' order by Artist, Title limit 100");
break;
case 'Artist Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Artist like '%$searchterm%' order by Title, Disc limit 100");
break;
case 'Title Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Title like '%$searchterm%' order by Artist, Disc limit 100");
break;
case 'Disc ID Search':
$searchresults=$db->query("select * from 1KaraokeDJ where Disc like '%$searchterm%' limit 100");
break;
}
if(mysqli_num_rows($searchresults) > 0) {
while($descri=mysqli_fetch_object($searchresults)) { ?>
<div class="reslt">
<h3 id="results">
<?php
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Artist);
echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Title);
?>
</h3>
<p class="Description">
<?php
echo $descri->Brand; echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Disc);
echo " - "; echo $descri->Track;
?>
</p>
<hr>
</div>
<?php } ?>
<div class="reslt">
<h3 id="results"><?php echo mysqli_num_rows($searchresults) ?> Results</h3>
<?php if(mysqli_num_rows($searchresults) >= 100) { ?>
<p class="Description highlight">Showing Up To 100 Results<br>Try Refining Your Search</p>
<?php } ?>
</div>
<?php } else { ?>
<div class="reslt">
<h3 id="results">Nothing Found!</h3>
<p class="Description highlight">Try Changing Your Search Terms<p>
<?php if ($_POST['searchbtn'] == "Disc ID Search" ) { ?>
Chartbuster = CBxxx<br>
DKKaraoke = DKxxx<br>
Karaoke Hits = KHxxx<br>
Sound Choice = SCxxx<br>
Sunfly = SFxxx
<?php } ?>
</div>
<?php }
} } ?>
</form>
<hr>
<code>© D & C Entertainment 2003 - 2015</code>
</div>
</body>
</html>
答案 0 :(得分:2)
尝试使用
SELECT DISTINCT discId FROM youTable WHERE itMatchsYourField ORDER BY artist
这只会返回您唯一的discId
所以你不会以相同的DK两次结束。现在在你的谜团中你有2个DKXXX
由于只有2个第一个字母匹配,你应该使用类似
的东西SELECT DISTINCT LEFT(discId , 2) FROM yourTable
这样你就不会得到两倍DK,你会获得SC或DK以及第一个出现的
答案 1 :(得分:0)
好的,经过更多的研究......似乎我需要使用内部联接?
select DISTINCT ID, Title, Brand from 1KaraokeDJ where Title like '%$searchterm%' AS temp
select * from 1KaraokeDJ inner join temp on 1KaraokeDJ.ID=temp.ID
但我仍然不知道如何正确地写这个...... select distinct仅返回一些数据。 我需要select *才能使用其余的数据。 因此,如果我在ID上加入唯一的*,结果应该是所有不是欺骗的数据。从理论上讲,我认为这是正确的。
那么我如何使用原始代码来理解我的理论呢?
<?php
$searchresults=$db->query("select * from 1KaraokeDJ where Title like '%$searchterm%' order by Artist limit 100");
if(mysqli_num_rows($searchresults) > 0) {
while($descri=mysqli_fetch_object($searchresults)) { ?>
<div class="reslt">
<h3 id="results">
<?php
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Artist);
echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Title);
?>
</h3>
<p class="Description">
<?php
echo $descri->Brand; echo " - ";
echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Disc);
echo " - "; echo $descri->Track;
?>
</p>
<hr>
</div>
<?php } }?>