如何改进此SQL查询(SQLite和PHP)
我正在基于SQLite中多个db表的数据呈现一个数组 表: staff-包含可以拥有证书的所有用户/工作人员 证书包含用户可以拥有的所有可用证书 rel\u staff\u certificate—包含与用户相关的证书 桌上工作人员 表格证书 表rel\uuuu员工证书 这就是交易:如何改进此SQL查询(SQLite和PHP),php,sql,sqlite,Php,Sql,Sqlite,我正在基于SQLite中多个db表的数据呈现一个数组 表: staff-包含可以拥有证书的所有用户/工作人员 证书包含用户可以拥有的所有可用证书 rel\u staff\u certificate—包含与用户相关的证书 桌上工作人员 表格证书 表rel\uuuu员工证书 这就是交易: // Get all available certificates $sql_get_cert = "SELECT * FROM certificates ORDER BY cert_id ASC"; //
// Get all available certificates
$sql_get_cert = "SELECT * FROM certificates ORDER BY cert_id ASC";
// Get all certificates for staff user
$sql_get_staff_cert_rel = "SELECT * FROM rel__staff_certificate WHERE staff_id = :staff_id AND cert_id = :cert_id LIMIT 1";
// Prepare SQL queries...
$get_cert_rel = $PDODB->prepare($sql_get_staff_cert_rel);
$get_cert = $PDODB->prepare($sql_get_cert);
// Get all certificates to array $certdata
$get_cert->execute();
$certdata = $get_cert->fetchAll();
// Create the array where I want to push data to:
$staffdata[$i] = array(
$staff['staff_id'], // $staff() is populated earlier and works just fine
$staff['name'],
$ccdata['region'], // $ccdata() is populated earlier and works just fine
$ccdata['cc_code']
);
// Loop through all available certificates in table ´certificates´
foreach ($certdata as $cert) {
// Bind values for the relation SQL now when we have them all
$get_cert_rel->bindValue(':staff_id', $staff['staff_id']);
$get_cert_rel->bindValue(':cert_id', $cert['cert_id']);
$get_cert_rel->execute();
// Get the certification relation data to array $certreldata()
$certreldata = $get_cert_rel->fetch();
// If a certificate date exists, then use that for our array which tells us that this staff has this certificate and was certified on this date
if (!empty($certreldata['cert_date'])) {
array_push($staffdata[$i], $certreldata['cert_date']);
}
// If no certificate date exsits, then just add value "N/A" as not available
else {
array_push($staffdata[$i], 'N/A');
}
}
好了,就这样!这是可行的,但正如您所看到的,我在foreach循环中对SQL server执行了大量SQL操作,我认为这不是很好,因为这将消耗大量不必要的时间
谁能告诉我如何用一个SQL查询来代替?或者告诉我如何改进代码以在其他方面进一步加快速度
谢谢。这可以通过加入来完成: 选择证书日期 从证书 使用证书id加入相关员工证书 其中staff_id=?
尝试使用SQL连接。下面是一个提供相关信息的示例: 选择 staff.staff\u id, staff.name, 证书.说明, 证书.说明, 相关人员证书。证书日期, rel_uuustaff_certificate.comments 来自rel____证书员工 certificates.cert\u id=rel\u staff\u certificate.cert\u id上的内部连接证书 staff.staff\u id=rel\u staff\u certificate.staff\u id上的内部加入staff 其中staff.staff_id=? 如果您希望所有员工在一次查询中拥有所有证书,则WHERE语句可能是可选的。您可以使用JOIN进行此类查询 使用左联接可以使用第一个表LEFT的私钥和第二个表right的外键联接两个表,而如果在第二个表right中找不到行,则右表的列将填充空值: 选择c* 从证书中选择c 在sc.cert\u id=c.cert\u id上以sc身份左加入相关人员证书 其中sc.staff_id=? 按c.cert\u id订购 使用内部联接可以使用与“左联接”相同的方式联接两个表,但如果右表中没有左表的私钥条目,则跳过这些行: 选择c* 从证书中选择c 在sc.cert\u id=c.cert\u id上作为sc的内部加入相关人员证书 其中sc.staff_id=? 按c.cert\u id订购
有关联接的更多信息,请参阅文档-基本结构和。使用联接如何?谢谢您的帮助!谢谢你的回答!谢谢你的回答!
`cert_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`caption` TEXT NOT NULL,
`description` TEXT
`rel_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`staff_id` INTEGER,
`cert_id` INTEGER,
`cert_date` TEXT NOT NULL,
`comments` TEXT
// Get all available certificates
$sql_get_cert = "SELECT * FROM certificates ORDER BY cert_id ASC";
// Get all certificates for staff user
$sql_get_staff_cert_rel = "SELECT * FROM rel__staff_certificate WHERE staff_id = :staff_id AND cert_id = :cert_id LIMIT 1";
// Prepare SQL queries...
$get_cert_rel = $PDODB->prepare($sql_get_staff_cert_rel);
$get_cert = $PDODB->prepare($sql_get_cert);
// Get all certificates to array $certdata
$get_cert->execute();
$certdata = $get_cert->fetchAll();
// Create the array where I want to push data to:
$staffdata[$i] = array(
$staff['staff_id'], // $staff() is populated earlier and works just fine
$staff['name'],
$ccdata['region'], // $ccdata() is populated earlier and works just fine
$ccdata['cc_code']
);
// Loop through all available certificates in table ´certificates´
foreach ($certdata as $cert) {
// Bind values for the relation SQL now when we have them all
$get_cert_rel->bindValue(':staff_id', $staff['staff_id']);
$get_cert_rel->bindValue(':cert_id', $cert['cert_id']);
$get_cert_rel->execute();
// Get the certification relation data to array $certreldata()
$certreldata = $get_cert_rel->fetch();
// If a certificate date exists, then use that for our array which tells us that this staff has this certificate and was certified on this date
if (!empty($certreldata['cert_date'])) {
array_push($staffdata[$i], $certreldata['cert_date']);
}
// If no certificate date exsits, then just add value "N/A" as not available
else {
array_push($staffdata[$i], 'N/A');
}
}