我对编码很陌生,并且出于我自己的兴趣,我正在尝试创建一个工作的数据库系统,并且随着我的进展而非常学习。
我有一个数据库查询,旨在显示Sample Name
,Tank Name
和Customer
匹配的最近结果。这些来自三个不同但相关的表格。
我提出了以下查询。 此查询工作但是对于我自己的教育,我是在正确的方向上工作还是有更好的方法来解决这个问题(这看起来非常不整洁,难以读懂我的想法)?
SELECT
`t_workorders`.`Work Order`,
`t_workorders`.`Date Sampled`,
`Result`,
`t_test_units`.`Unit`
FROM
`t_sampletests`
RIGHT JOIN `t_samples`
ON `Sample` = `pk_Sample`
RIGHT JOIN `t_workorders`
ON `t_samples`.`Work Order` = `t_workorders`.`Work Order`
LEFT JOIN `t_test_units`
ON `t_sampletests`.`Unit` = `t_test_units`.`pk_Unit`
WHERE `t_sampletests`.`Test` =
(SELECT
`Test`
FROM
`t_sampletests`
WHERE `pk_SampleTest` = $ testID)
AND `t_Samples`.`Sample Name` =
(SELECT
`Sample Name`
FROM
`t_sampletests`
LEFT JOIN `t_Samples`
ON `Sample` = `pk_Sample`
WHERE `pk_SampleTest` = $ testID)
AND `t_Samples`.`Tank` =
(SELECT
`Tank`
FROM
`t_sampletests`
LEFT JOIN `t_Samples`
ON `Sample` = `pk_Sample`
WHERE `pk_SampleTest` = $ testID)
AND `t_workorders`.`Customer` =
(SELECT
`Customer`
FROM
`t_sampletests`
LEFT JOIN `t_Samples`
ON `Sample` = `pk_Sample`
LEFT JOIN `t_workorders`
ON `t_Samples`.`Work Order` = `t_workorders`.`Work Order`
WHERE `pk_SampleTest` = $ testID)
AND `t_workorders`.`Work Order` <>
(SELECT
`t_workorders`.`Work Order`
FROM
`t_sampletests`
LEFT JOIN `t_Samples`
ON `Sample` = `pk_Sample`
LEFT JOIN `t_workorders`
ON `t_Samples`.`Work Order` = `t_workorders`.`Work Order`
WHERE `pk_SampleTest` = $ testID)
ORDER BY `t_workorders`.`Work Order` DESC
LIMIT 15
非常感谢任何替代或更合适的方法的建议。
非常感谢
答案 0 :(得分:0)
这样的事情:
select feild1, field2 from
(select feild1, field2 from table1)as a
left join
select field1, desc from table2) as b
on a.field1 = b.field1
这将优化您的查询
避免选择您未使用它的表格中的所有字段。仅选择要显示为优化的字段
答案 1 :(得分:0)
IMO,使用新行和增量将帮助您阅读和构建查询,并让您快速了解它的作用。
我还尝试在使用SQL关键字时保持一致,并以大写形式写出来。
SELECT `t_workorders`.`Work Order`, `t_workorders`.`Date Sampled`, `Result`,`t_test_units`.`Unit`
FROM `t_sampletests`
RIGHT JOIN `t_samples` ON `Sample` = `pk_Sample`
RIGHT JOIN `t_workorders` ON `t_samples`.`Work Order` = `t_workorders`.`Work Order`
LEFT JOIN `t_test_units`ON `t_sampletests`.`Unit` = `t_test_units`.`pk_Unit`
WHERE `t_sampletests`.`Test` = (
SELECT `Test` FROM `t_sampletests` WHERE `pk_SampleTest` = $testID
)
AND `t_Samples`.`Sample Name` = (
SELECT `Sample Name`
FROM `t_sampletests`
LEFT JOIN `t_Samples` ON `Sample` = `pk_Sample` WHERE `pk_SampleTest` = $testID
)
AND `t_Samples`.`Tank` = (
SELECT `Tank`
FROM `t_sampletests`
LEFT JOIN `t_Samples` ON `Sample` = `pk_Sample` WHERE `pk_SampleTest` = $testID
)
AND `t_workorders`.`Customer`= (
SELECT `Customer`
FROM `t_sampletests`
LEFT JOIN `t_Samples` ON `Sample` = `pk_Sample`
LEFT JOIN `t_workorders` ON `t_Samples`.`Work Order` = `t_workorders`.`Work Order`
WHERE `pk_SampleTest` = $testID
)
AND `t_workorders`.`Work Order` <> (
SELECT `t_workorders`.`Work Order`
FROM `t_sampletests`
LEFT JOIN `t_Samples` ON `Sample` = `pk_Sample`
LEFT JOIN `t_workorders` ON `t_Samples`.`Work Order` = `t_workorders`.`Work Order`
WHERE `pk_SampleTest` = $testID
)
ORDER BY `t_workorders`.`Work Order`
DESC LIMIT 15;
(并且不要忘记查询后面的;
)