gpt4 book ai didi

php - 在 CodeIgniter 中查询两个表

转载 作者:行者123 更新时间:2023-11-29 22:54:16 25 4
gpt4 key购买 nike

我正在尝试查询两个不同的表,看看是否有一个匹配项。我已经做了很多关于如何做到这一点的研究,我一直看到的是使用 join 子句。但我并不是在寻找两个表之间的匹配。我只需要查询两个表并查看是否有一行匹配。

这是我正在使用的代码。

     $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));

关于php - 在 CodeIgniter 中查询两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com