我使用五个表来存储项目数据。创建新作业时,我首先要求我的脚本搜索以查看是否存在具有相同项目编号的项目。这些表中的每一个都使用列“project_number”作为索引键
以下是我试图用来同时搜索五个表的内容,但是它无法返回我知道的结果。
$check_project_number = "SELECT * FROM project_details WHERE project_number = ?
UNION
SELECT * FROM project_crews WHERE project_number = ?
UNION
SELECT * FROM project_tooling WHERE project_number = ?
UNION
SELECT * FROM project_scope WHERE project_number = ?
UNION
SELECT * FROM project_equipment WHERE project_number = ?";
$q_check_project_number = $conn->prepare($check_project_number);
$q_check_project_number ->execute(array($project_number,$project_number,$project_number,$project_number,$project_number));
$project_count = $q_check_project_number ->rowCount();
if ($project_count > 0) /*if a user exists*/
{
print "project exists";
}
else
{ /*insert records*/
答案 0 :(得分:0)
使用UNION
时,SELECT
语句中指定的列必须与您使用UNION
标记的所有不同查询完全匹配。如果它们中的任何一个不同,它将失败。我的假设是你要查询的一个或多个表有1)其他表中不存在的列,或2)不同的列名
示例(当然,更改select子句以匹配您需要从表中提取的列名:
SELECT project_id, project_number, project_name FROM project_details WHERE project_number = ?
UNION
SELECT project_id, project_number, project_name FROM project_crews WHERE project_number = ?
UNION
SELECT project_id, project_number, project_name FROM project_tooling WHERE project_number = ?
UNION
SELECT project_id, project_number, project_name FROM project_scope WHERE project_number = ?
UNION
SELECT project_id, project_number, project_name FROM project_equipment WHERE project_number = ?