0

我是 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 | 转数 | 点火正时 | 马力|

| 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 | 转数 | 点火正时 | 马力|
| 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 |

获取上表的正确查询是什么?提前致谢!

4

2 回答 2

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 都是某处的主键或外键。

希望这可以帮助。在旁注中查看我示例中的连接语法,它被认为是新标准,因为您编写连接的方式有点过时但没有错。

于 2012-12-07T08:56:47.433 回答
0

只需要一组/ DISCTINCT

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;

或者

  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;
于 2012-12-07T08:49:08.013 回答