我有一个 sql 查询,我检查一个值是否在表的最大值和最小值之间。我现在按如下方式实现:
SELECT spectrum_id, feature_table_id
FROM 'spectrum', 'feature'
WHERE `spectrum`.msrun_msrun_id = 1
AND `feature`.msrun_msrun_id = 1
AND (SELECT min(rt) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`.feature_table_id) <= scan_start_time
AND scan_start_time <= (SELECT max(rt) FROM `convexhull` WHERE 'convexhull'.feature_feature_table_id = 'feature'.feature_table_id)
AND (SELECT min(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`.feature_table_id) <= base_peak_mz
AND base_peak_mz <= (SELECT max(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`feature_table_id)
这运行非常缓慢,因为每次运行此查询时我都会从凸包中选择 4 次,因此我尝试使用内部连接对其进行改进:
SELECT spectrum_id, feature_table_id
FROM 'spectrum', 'feature'
INNER JOIN `convexhull` ON `convexhull`.feature_feature_table_id = `feature`.feature_table_id
WHERE `spectrum`.msrun_msrun_id = ? "+
AND `feature`.msrun_msrun_id = ? "+
AND min(`convexhull`.rt) <= scan_start_time "+
AND scan_start_time <= max(`convexhull`.rt) "+
AND min(`convexhull`.mz) <= base_peak_mz "+
AND base_peak_mz <= max(`convexhull`.mz)", spectrumFeature_InputValues)
但是,min() 和 max() 语句只能在 select 语句之后使用。如何使第一个查询更有效率,这样我就可以得到 min 和 max rt 和 mz 而不必进行 4 次查询?