我的查询和其中一个连接有问题,初始查询工作正常但是当我尝试加入链接表时,每个文章有4个匹配的链接,查询返回每个文章的4条记录,从而显示我的网站上每篇文章4次?
所以我的表是:
tbl_news_articles => **newsID**, newsTitle, newsBody, newsDate
tbl_images => **imageID**, filename
tbl_news_images => **imageID**, **newsID**
tbl_links => **linkID**, linkTitle, linkURL
tbl_news_links => **newsID**, **linkID**
我正在使用的查询是
SELECT n.newsID, n.type, n.title, n.body, n.date, ni.imageID, i.imageID, i.filename, l.linkID, l.linkTitle, l.linkURL
FROM tbl_news_articles AS n
LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID
LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
LEFT JOIN tbl_news_links AS nlink ON nlink.newsID = n.newsID
LEFT JOIN tbl_links AS l ON l.linkID = nlink.linkID
返回类似下面的内容
newsID, newsTite, newsDesc, newsDate, imageID, filename, linkID, linkTitle, linkURL
65,Article 1 Title,Article 1 description,2016-05-21,1,group.jpg,1,link1 title,linkURL
65,Article 1 Title,Article 1 description,2016-05-21,1,group.jpg,2,link2 title,linkURL
65,Article 1 Title,Article 1 description,2016-05-21,1,group.jpg,3,link3 title,linkURL
65,Article 1 Title,Article 1 description,2016-05-21,1,group.jpg,4,link4 title,linkURL
所以每篇文章每个链接返回4次。
解决此问题的正确方法是什么?我应该在我的PHP代码中运行一个单独的查询来单独获取链接吗?
感谢您的帮助
我的PHP代码是
<?php
//call the database to retreive the records
$sql = "SELECT n.newsID, n.type, n.title, n.body, n.date, ni.imageID, i.imageID, i.filename, l.linkID, l.linkTitle, l.linkURL FROM tbl_news_articles AS n
LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID
LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
LEFT JOIN tbl_news_links AS nlink ON nlink.newsID = n.newsID
LEFT JOIN tbl_links AS l ON l.linkID = nlink.linkID
ORDER BY date DESC LIMIT $offset, $rowsperpage";
$result = $conn->query($sql);
$i = 0;
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$id = $row['newsID'];
$link = "article.php?id=".$id;
$type = $row['type'];
$title = $row['title'];
$urltitle = strtolower($title);
$urltitle = str_replace(" ","-",$urltitle);
$body = $row['body'];
#$bodytext = (strlen($body) > 130) ? substr($body,0,130).'...' : $bodytext;
$pos= strpos($body, ' ', 140);
$bodytext = substr($body,0,$pos);
$bodytext .= "... <a href='$link' title='$title'>read more</a>";
$date = $row['date'];
$formated_date = date("d-M-Y", strtotime($date));
$imgID = $row['imageID'];
$filename = $row['filename'];
if($filename != "")
{
$imgLink = "images/news-articles/".$id."/".$filename;
}
else
{
$imgLink = "images/news-item-placeholder.jpg";
}
$i++;
if($i == 1)
{
echo "<div class='news-spotlight'>";
}
elseif($i >=2 && $i <=3)
{
if($i == "2")
{
$class = "first";
}
else
{
$class = "";
}
echo "<div class='news-highlight $class'>";
}
else
{
echo "<div class='news-item'>";
}?>
<a itemprop="url" href="<?php echo $link?>"><img itemprop="image" src="<?php echo $imgLink?>" alt="<?php echo $title?>" title="<?php echo $title?>"></a>
<div class='data'>
<h3><a href="<?php echo $link?>"><span itemprop="name"><?php echo $title?></span></a></h3>
<div class="article-date"><?php echo $formated_date?></div>
<span itemprop="startDate" content="<?php echo $date?>"></span>
<div itemprop="location" itemscope itemtype="http://schema.org/Place">
<span itemprop="address" content="England"></span>
<span itemprop="name" content="MayoShinDo Association"></span>
</div>
<p itemprop="description"><?php echo $bodytext?></p>
<?php if($i == 1)
{?>
<ul>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 1</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 2</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 3</a></li>
<li><a href="<?php echo $link?>"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> link 4</a></li>
</ul><?php
}?>
</div><?php
echo "</div>";
}
}
else
{
echo "0 Results";
}?>
我还没有在php代码中设置链接,正如您从上面的代码中看到的那样,只是想先查询正确的
我看过你可以使用GROUP_CONCAT吗?我尝试了以下
SELECT n.newsID, n.type, n.title, n.body, n.date, ni.imageID, i.imageID, i.filename, l.linkID, l.linkTitle, l.linkURL
GROUP_CONCAT(("|", l.linkID,l.linkTitle,l.linkURL)) as "fieldvalue"
FROM tbl_news_articles AS n
LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID
LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
LEFT JOIN tbl_news_links AS nlink ON nlink.newsID = n.newsID
LEFT JOIN tbl_links AS l ON l.linkID = nlink.linkID
但是我说我的sql语法有错误?任何想法
谢谢你!