从多个表和多个喜欢中选择查询

时间:2015-02-16 23:32:28

标签: php mysql sql

我试图只用一个查询字符串查询几个表而没有运气。我在这里读了50篇帖子但没有用。这就是我到目前为止所拥有的:

$sql = "SELECT *
    FROM table1
    WHERE (id LIKE :searchterm
    OR fname LIKE :searchterm
    OR lname LIKE :searchterm)
    UNION ALL
    SELECT *
    FROM table2
    WHERE (id LIKE :searchterm
    OR lang1 LIKE :searchterm
    OR lang2 LIKE :searchterm)
    ";
try
{
    $core = Core::getInstance();
    $stmt = $core->dbh->prepare($sql);
    $stmt->bindParam(':searchterm', $this->_searchTerm);
    if ($stmt->execute()) {
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
        while ($result = $stmt->fetch()) {
            print_r($result);
        }
    } else {
        echo "Couldn't get result.";
    }
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

如果我从UNION ALL中删除并只留下第一个SELECT,它就可以了。有什么想法吗?

0 个答案:

没有答案