0

私は mySQL を初めて使用するので、次のクエリでいくつかの助けを借りることができます。

エンジン設定を実験番号とエンジン rpm に関連付けるテーブルと、エンジン パフォーマンスを実験番号とエンジンに関連付ける別のテーブルがあります。

ここに私がやっているクエリがあります

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 | 点火タイミング | 馬力 |

| | 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 行を見てみましょう。実験 2 の Ignition_timing は実際には 24.00 であり、実験 1 では 24.5 です。では、なぜ実験 2 で両方の値が得られるのでしょうか?

これが私が得たい結果です。. .

| | exp_no | rpm | 点火タイミング | 馬力 |
| | 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

Your query is not joining on all required field. You are only joining on the rpm fields your query and it appears that the join is incomplete.

SELECT performance.exp_no, performance.rpm, ignition_timing, horse_power
FROM engine_settings, performance WHERE performance.rpm = engine_settings.rpm
ORDER BY rpm;

You should change your query to the following:

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;

As I dont have the table structures I might be wrong but I suspect that both rpm and exp_no is a primary key or foreign key somewhere.

Hope this helps. On a side note look at the join syntax in my example it is considered the new standard the way you are writting a join is a bit outdates but not wrong.

于 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 に答える