高级MySQLi查询语法

时间:2017-05-31 12:59:05

标签: mysqli prepared-statement

我有一个相当复杂的mysqli insert语句要做,因为我需要从其他两个表中收集数据,并且在一个语句中也使用php中可用的一些变量。

由于准备好的陈述的变量顺序,这让我感到困惑。 - 我只插入四个值,但为了获取t1和t2的值,我在bind_param语句中也引用了这三个附加变量。

我应该用$ newJobRoleID,$ newCompanyID,t1.id,t2.id

填充jobRoleID,companyID,departmentID,capabilityID
$addJobCompetencies = $con->prepare("
INSERT INTO jobRoleCompetencies (jobRoleID,companyID,departmentID,competenceID)
WITH t1 AS (SELECT id FROM departments WHERE companyID = ? AND department = ?), 
     t2 AS (SELECT id FROM competencies WHERE companyID = ?)
     SELECT ?,?,t1.id,t2.id FROM t1,t2");

if($addJobCompetencies) {
    $addJobCompetencies->bind_param('isiii', $newCompanyID,$row['department'],$newCompanyID,$newJobRoleID,$newCompanyID);
    $addJobCompetencies->execute();
} else {
    echo $con->error;   
}
$addJobCompetencies->free_result();

我收到语法错误,但我不能100%确定原因。有人可以帮忙吗?

由于 丹

1 个答案:

答案 0 :(得分:0)

我还没有解决这个查询的语法问题,但是通过简单地组合select语句来检索单个select语句中我需要的所有信息......

SELECT?,?, departmentss.id AS departmentID,competencies.id AS competencyID FROM department,competencies ... etc