我有3个查询,这些查询花费太多时间来执行并将结果加载到页面(slickgrid)中,因此我想使用联接将其优化为单个查询。
我无法获得有关如何实现此目标的想法。你们中的任何人都可以在这里指导我如何从以下3个查询中获取单个查询。
$ikt = $row['2'];
$id = $row['0'];
$record = $row['1'];
$sql2 = "SELECT * FROM `plant_records` WHERE IKT = '".$ikt."' AND Record='".$record."'";
$adddetails2 = mysql_query($sql2);
$recorddetails2 = mysql_fetch_assoc($adddetails2);
$num_rows3 = mysql_num_rows($adddetails2);
if($num_rows3 > 0){
$storeData[$i][] ='Filled';
}
else{
$storeData[$i][] ='Not Filled';
}
$sql5= "SELECT * FROM `plant_overview` WHERE IKT = '".$ikt."' AND ID_Record='".$record."'";
$adddetails5 = mysql_query($sql5);
$recorddetails5 = mysql_fetch_assoc($adddetails5);
$num_rows6 = mysql_num_rows($adddetails5);
if($num_rows6 > 0){
$storeData[$i][] ='Filled';
}
else{
$storeData[$i][] ='Notfilled';
}
$sql3 = "SELECT * FROM `plant_info` WHERE TG_ID = $id and Present != '' ";
$adddetails3 = mysql_query($sql3);
$recorddetails3 = mysql_fetch_assoc($adddetails3);
$num_rows4 = mysql_num_rows($adddetails3);
if($num_rows4 > 0){
$storeData[$i][] ='Filled';
}
else{
$storeData[$i][] ='Notfilled';
}
Status: listdata[i]['16'],
Records: listdata[i]['17'],
Checking: listdata[i]['18'],
我需要不同列表数据中的结果,因为我必须根据那里的结果显示不同的数据。
这是我的表结构。
master_table
ID (int - PK)
Record (varchar)
IKT (varchar)
( The below are refering to the above table <br>
$ikt = $row['2'];
$id = $row['0'];
$record = $row['1'];
)
plant_records
ID (int - PK)
IKT (varchar)
Record (varchar)<
Student (varchar)
plant_overview
ID (int - PK)
IKT (varchar)
ID_Record (varchar)
Subject (varchar)
plant_info
ID (int - PK)
TG_ID (int - this is refering to the ID column in master table)<br>
Present (varchar)
result (varchar)
评论的问题2:我将如何使用其他listdata中的数据
答案:在slickgrid中,我有Plant_Records,Plant_Overview,Plant_Info的列
基于数据(填充/未填充),我将在slickgrid中显示不同的图像
例如,在Slickgrid的Plant_Record列中。如果该值被填充,我将显示图像A,如果该值是“未填充”,我将显示图像B
在Slickgrid的Plant_overview列中。如果该值被填充,我将显示图像C,如果该值是“未填充”,我将显示图像D。
答案 0 :(得分:1)
您是否只想检查表中是否存在匹配项?您可以使用EXISTS
或IN
进行此操作。
带有EXISTS子句:
select
id, record, ikt,
case when exists (select * from plant_records pr where pr.ikt = mt.ikt and pr.record= mt.record)
then 'Filled' else 'Not filled' end as pr_filled,
case when exists (select * from plant_overview po where po.ikt = mt.ikt and po.record= mt.record)
then 'Filled' else 'Not filled' end as po_filled,
case when exists (select * from plant_info pi where pi.tg_id = mt.id and pi.present <> '')
then 'Filled' else 'Not filled' end as pi_filled
from master_table mt;
带有IN子句:
select
id, record, ikt,
case when (ikt, record) in (select ikt, record from plant_records)
then 'Filled' else 'Not filled' end as pr_filled,
case when (ikt, record) in (select ikt, record from plant_overview)
then 'Filled' else 'Not filled' end as po_filled,
case when (id) in (select tg_id from plant_info where present <> '')
then 'Filled' else 'Not filled' end as pi_filled
from master_table;
添加where id = $id
(如果您只想用于一行)。
您应该具有以下索引:
create index idx1 on plant_records (ikt, record);
create index idx2 on plant_overview (ikt, record);
create index idx3 on plant_info (tg_id, present);
create index idx4 on plant_info (present, tg_id);
将仅使用idx3或idx4。您可以检查哪个并删除另一个。 (当然,DBMS可能不使用任何索引。它们只是报价。DBMS将决定是否使用它们。)