是否有可能优化我编写的查询
我创建了一种动态虚拟数据库,以使用户能够添加自定义字段而不影响数据库结构。到目前为止,这是该结构的非常简化的视图。
tables | columns
db_cases | caseid db_structure | fieldname db_data | fieldname, data, caseid db_names | nameid
我正在尝试将案例输出到html表
希望其余的内容可以自我解释,您可以看到它的效率如何。我可以通过联接做同样的事情吗?
SELECT case_id, (SELECT data_field_value FROM db_data WHERE data_case_id = case_id AND data_field_name = 'casestatus' ) AS casestatus, (SELECT forename_company FROM db_names WHERE name_id = (SELECT data_field_value FROM db_data WHERE data_case_id = case_id AND data_field_name = 'client1' ) ) AS client1_forename_company FROM db_cases
谢谢
实际上,Chibuzo是正确的。首先删除它:-))但是在此之前,先玩一下,这是很好的大脑锻炼,例如象棋之类的东西:-)
select case_id, d_status.data_field_value as case_status, d_client1_name.forename_company as client1_forename_company from db_cases join db_data as d_status on d_status.data_case_id = case_id AND d_status.data_field_name = 'casestatus' join db_data as d_client1 on d_client1.data_case_id = case_id AND d_client1.data_field_name = 'client1' join db_names as d_client1_name on d_client1_name.name_id = d_client1.data_field_value
我希望没有子查询的这些直接联接会更加高效,尽管您必须对其进行测试-优化经常会让人感到意外。