对两个表执行选择,结果允许访问这两个选定表的字段

时间:2014-07-13 19:05:33

标签: php mysql sql pdo

我正在做一个搜索系统,我遇到了一些问题。

我需要搜索两个表(新闻和页面),我已经成功地只为一个表执行搜索系统,但是对于两个表,这不容易做到。

我已经使用带有两个使用UNION的表的select语句,因为我想显示搜索结果的数量,即我的第一个sql语句的返回行数。

但现在我需要做一个选择状态,允许我访问我的新闻表的所有字段和我的页面表的所有字段。

我需要在我的新闻表中访问此字段:id,title,content,link,date,nViews

我需要在我的页面表中访问此字段:id,title,content,link

我也试图用UNION做这个,但在这种情况下我没有任何行返回。

您是否在我的代码中看到了我的错误?

<?php
//first I get my $search keyword
$search = $url[1];

$pdo = connecting();
//then I want to show number of returned rows for keyword searched
$readALL = $pdo->prepare("SELECT title,content FROM news WHERE title LIKE ? OR content LIKE ? 
                          UNION SELECT title,content FROM pages WHERE title LIKE ? OR content like ?");
$readALL->bindValue(1,"%$search%", PDO::PARAM_STR);
$readALL->bindValue(2,"%$search%", PDO::PARAM_STR);
$readALL->bindValue(3,"%$search%", PDO::PARAM_STR);
$readALL->bindValue(4,"%$search%", PDO::PARAM_STR);
$readALL->execute();
//I show number of returned rows
echo '<p>Your search keyword returned <strong>'.$readALL->rowCount().'</strong> results!</p>';
//If dont return any rows I show a error message
if($readALL->rowCount() <=0){
    echo 'Sorry but we didnt found any result for your keyword search.';
}
else{
    //If return rows I want to show, if it is a page result I want to show title and link that I have in my page table
    //if it is a news result I want to show title and link that I have in my news table and also date of news
    echo '<ul class="searchlist">';
            $readALL2 = $pdo->prepare("SELECT * FROM news WHERE status = ? AND title LIKE ? OR content LIKE ? LIMIT 0,4
                                      UNION SELECT * FROM pages where title LIKE ? OR content LIKE ? LIMIT 0,4"); 
            $readALL2->bindValue(1, '1');
            $readALL2->bindValue(2, "%$search%", PDO::PARAM_STR);
            $readALL2->bindValue(3, "%$search%", PDO::PARAM_STR);
            $readALL2->bindValue(4, "%$search%", PDO::PARAM_STR);
            $readALL2->execute();   

            while ($result = $readALL2->fetch(PDO::FETCH_ASSOC)){
                echo '<li>';
                    echo '<img src="'.BASE.'/uploads/news/'.$result['thumb'].'"/>';
                    echo '<a href="'.BASE.'/news/'.$result['id_news'].'">'.$result['title'].'</a>';
                    //if it is a news result I also want to show data on my list
                    //echo '<span id="date">'.$result['data'].'</span>';
                echo '</li>';
            }
    echo ' </ul>';
    //but how can I do my select statement to have access to my news table fields and my page table fields??
}
?>

这是我的新闻表:

enter image description here

这是我的页面表:

enter image description here

当我在表单上搜索关键字&#34; doc&#34;我明白了:

您正在搜索关键字:&#34; doc&#34;

您的搜索返回了2个结果!

数组([id_news] =&gt; 472 [thumb] =&gt; 2014/07 / title-of-news-11405372264.png [title] =&gt; Documents [content] =&gt;链接1,链接2 [ ofte] =&gt; 2014-07-14 23:11:04 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 116 [状态] =&gt; 1 [id] =&gt; 1 [link] =&gt;文件)新闻文件图片

数组([id_news] =&gt; 473 [thumb] =&gt; 2014/07 / title-of-news-21405372282.png [title] =&gt; Documents [content] =&gt;链接1,链接2 [ ofte] =&gt; 2014-07-14 23:11:22 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 115 [状态] =&gt; 1 [id] =&gt; 1 [link] =&gt;文件)新闻文件图片

数组([id_news] =&gt; 472 [thumb] =&gt; 2014/07 / title-of-news-11405372264.png [title] =&gt;关于[content] =&gt;我们是公司.. 。[ofte] =&gt; 2014-07-14 23:11:04 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 116 [状态] =&gt; 1 [id] =&gt ; 2 [link] =&gt; about)新闻图片关于

数组([id_news] =&gt; 473 [thumb] =&gt; 2014/07 / title-of-news-21405372282.png [title] =&gt;关于[content] =&gt;我们是公司.. 。[ofte] =&gt; 2014-07-14 23:11:22 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 115 [状态] =&gt; 1 [id] =&gt ; 2 [link] =&gt; about)新闻图片关于

数组([id_news] =&gt; 472 [thumb] =&gt; 2014/07 / title-of-news-11405372264.png [title] =&gt;联系人[content] =&gt;电子邮件:test @ email。 com [ofte] =&gt; 2014-07-14 23:11:04 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 116 [状态] =&gt; 1 [id] =&gt ; 3 [link] =&gt;联系人)新闻联系人图片

数组([id_news] =&gt; 473 [thumb] =&gt; 2014/07 / title-of-news-21405372282.png [title] =&gt;联系人[content] =&gt;电子邮件:test @ email。 com [ofte] =&gt; 2014-07-14 23:11:22 [views] =&gt; 0 [作者] =&gt; 1 [类别] =&gt; 115 [状态] =&gt; 1 [id] =&gt ; 3 [link] =&gt;联系人)新闻联系人图片

1 个答案:

答案 0 :(得分:1)

要查询2个表,并让结果集包含两个表中的列:

SELECT n.id, n.status, n.views,  p.id, p.title
FROM news n, pages p
WHERE n.status = ? 
AND p.title = ?
...

为简化我的回答,我省略了大部分必需的列,但您只需在select语句中添加更多内容。当然,您可以随时使用

SELECT n.*, p.*

从两个表中选择所有列。

更新

针对您的具体方案,请尝试:

SELECT n.*, p.* 
FROM news n, pages p
WHERE n.title LIKE ?
  OR n.content LIKE ?
  OR p.title LIKE ?
  OR p.content LIKE ?