我有 3 个表:用户、医生、care_provider,它们都有用户名和电子邮件字段。
我需要确保为 3 种用户类型中的任何一种创建的用户名和电子邮件在所有 3 个表中都是唯一的。
有没有办法连接或联合表来检查每个表中是否有相同的数据?
显然我可以进行 3 个选择,但当然我更愿意通过单个查询来检查用户名。
现在我只是像这样检查所有 3 个:
$count = 0;
$user = $this->db->query("
SELECT COUNT(user_id) AS total
FROM " . DB_PREFIX . "user
WHERE username = '" . $this->db->escape($username) . "'
");
if ($user->row['total']):
$count += $user->row['total'];
endif;
... (for each table) ...
return $count;
最佳答案
SELECT COUNT(*) AS total FROM table1, table2, table3
WHERE table1.username = input
OR table2.username = input
OR table3.username = input
或者你可以做类似这个问题的事情。 mySQL select count, multiple tables
关于php - 从多个表中选择相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921859/