在CodeIgniter中查询两个表


Querying two tables in CodeIgniter

我只是想查询两个不同的表,看看是否有一个匹配。我已经做了很多关于如何做到这一点的研究,我一直看到的是使用联接子句。但我不想在两张桌子之间找到一个匹配的。我只需要查询两个表,看看是否有一行匹配。

这是我正在使用的代码。

     $query = " SELECT id,account_type,email, password  FROM client
              WHERE email = ?
              UNION
            SELECT id,account_type,email, password  FROM freelancers
            WHERE email = ?  ";

    $email = $this->input->post("email");
    $result = $this->db->query($query, [$email]);
    // Just check if first there is an email that exists the  database
    if($result -> num_rows() == 1)  {
      //Do something
    }

您可以尝试使用子查询:

$query = "SELECT * FROM client WHERE email = (SELECT email FROM freelancers WHERE email = ?)";
$email = $this->input->post("email");
$result = $this->db->query($query, array($email));