我是mySQL的新手,我可以在以下查询中使用一些帮助。
我有一个表格,将发动机设置与实验编号和引擎转速相关联, 以及另一个将发动机性能与实验数和发动机相关联的表格。
这是我正在做的查询
SELECT performance.exp_no, performance.rpm, ignition_timing, horse_power
FROM engine_settings, performance
WHERE performance.rpm = engine_settings.rpm
ORDER BY rpm;
| exp_no | rpm | ignition_timing | horse_power |
| 2 | 1000 | 24.50 | 105.23 |
| 2 | 1000 | 24.00 | 105.23 |
| 1 | 1000 | 24.50 | 100.23 |
| 1 | 1000 | 24.00 | 100.23 |
| 1 | 2000年| 25.50 | 125.03 |
| 1 | 2000年| 25.00 | 125.03 |
| 2 | 2000年| 25.50 | 129.03 |
| 2 | 2000年| 25.00 | 129.03 |
| 1 | 3000 | 26.50 | 154.65 |
| 1 | 3000 | 26.00 | 154.65 |
| 2 | 3000 | 26.50 | 159.65 |
| 2 | 3000 | 26.00 | 159.65 |
| 1 | 4000 | 27.50 | 178.23 |
| 1 | 4000 | 27.00 | 178.23 |
| 2 | 4000 | 27.50 | 184.23 |
| 2 | 4000 | 27.00 | 184.23 |
| 2 | 5000 | 28.50 | 195.36 |
| 2 | 5000 | 28.00 | 195.36 |
| 1 | 5000 | 28.50 | 189.36 |
| 1 | 5000 | 28.00 | 189.36 |
20行(0.06秒)
现在以前两行为例。实验2的Ignition_timing实际上是24.00,而对于实验1,它是24.5。那么为什么我得到实验2的两个值呢?
以下是我希望获得的结果。 。
| exp_no | rpm | ignition_timing | horse_power |
| 2 | 1000 | 24.00 | 105.23 |
| 1 | 1000 | 24.50 | 100.23 |
| 1 | 2000年| 25.50 | 125.03 |
| 2 | 2000年| 25.00 | 129.03 |
| 1 | 3000 | 26.50 | 154.65 |
| 2 | 3000 | 26.00 | 159.65 |
| 1 | 4000 | 27.50 | 178.23 |
| 2 | 4000 | 27.00 | 184.23 |
| 2 | 5000 | 28.00 | 195.36 |
| 1 | 5000 | 28.50 | 189.36 |
获取上表的正确查询是什么?提前谢谢!
答案 0 :(得分:0)
只需要/ DISTINCT所需的小组
SELECT
performance.exp_no,
performance.rpm,
ignition_timing,
DISTINCT(horse_power)
FROM
engine_settings
inner join performance ON performance.rpm = engine_settings.rpm
ORDER BY rpm;
OR
SELECT
performance.exp_no,
performance.rpm,
ignition_timing,
horse_power
FROM
engine_settings
inner join performance ON performance.rpm = engine_settings.rpm
GROUP BY horse_power
ORDER BY rpm;
答案 1 :(得分:0)
您的查询未加入所有必填字段。您只是在查询的rpm字段上加入,并且看起来连接不完整。
SELECT performance.exp_no, performance.rpm, ignition_timing, horse_power
FROM engine_settings, performance WHERE performance.rpm = engine_settings.rpm
ORDER BY rpm;
您应该将查询更改为以下内容:
SELECT performance.exp_no, performance.rpm, ignition_timing, horse_power
FROM engine_settings INNER JOIN performance
ON engine_setting.rpm=performance.rpm
AND engine_Settings.exp_no = performance.exp_no
ORDER BY rpm;
因为我没有表结构,所以我可能错了,但我怀疑rpm和exp_no都是某个主键或外键。
希望这会有所帮助。在旁注中看一下我的例子中的连接语法,它被认为是新标准,你写连接的方式有点过时但没有错。