Tabellenstruktur der Datenbank:
Coach TabelleJoin Rückkehr falsche Werte
+----+-----------+-------+
| ID | Name | Age |
+----+-----------+-------+
| 1 | Aaron | 39 |
| 2 | John | 41 |
| 3 | Macy | 44 |
| 4 | Mitchelle | 37 |
| 5 | Candice | 32 |
+----+-----------+-------+
Trainee Tisch
+----+---------+-------+
| ID | Name | Age |
+----+---------+-------+
| 6 | Abigail | 9 |
| 7 | Jim | 12 |
| 8 | Jack | 7 |
| 9 | Maria | 14 |
| 10 | Andy | 11 |
+----+---------+-------+
Gebühren-Tabelle
+----+----------+------------+--------+
| ID | Coach_ID | Trainee_ID | Fee |
+----+----------+------------+--------+
| 11 | 1 | 7 | 2400 |
| 12 | 2 | 6 | 2000 |
| 13 | 3 | 6 | 2000 |
| 14 | 4 | 8 | 1243 |
| 15 | 5 | 8 | 1275 |
| 16 | 3 | 9 | 9010 |
| 17 | 2 | 8 | 1900 |
| 18 | 1 | 7 | 600 |
| 19 | 2 | 10 | 1010 |
| 20 | 5 | 10 | 2110 |
+----+----------+------------+--------+
erforderliche Ausgabe
+-----------+--------------+--------+
| Name | Trainee_name | Fee |
+-----------+--------------+--------+
| Aaron | Jim | 2400 |
| Candice | Andy | 2110 |
| John | Abigail | 2000 |
| Macy | Maria | 9010 |
| Mitchelle | Jack | 1243 |
+-----------+--------------+--------+
=====
Mein QUERY
Select
Coach.Name as Name, Trainee.Name as Trainee_name, Max(Fee.Fee) as Fee
From Fee
INNER JOIN Trainee
ON Fee.Trainee_ID = Trainee.ID
INNER JOIN Coach
ON Fee.Coach_ID = Coach.ID
GROUP BY Coach.Name
ORDER BY Coach.Name;
Meine Leistung:
+------------+---------------+--------+
| Name | Trainee_name | Fee |
+------------+---------------+--------+
| Aaron | Jim | 2400 |
| Candice | Jack | 2110 |
| John | Abigail | 2000 |
| Macy | Abigail | 9010 |
| Mitchelle | Jack | 1243 |
+------------+---------------+--------+
Die Daten von Candice und Macy in trainee_name Spalte sind falsch entspricht.
Dump von SQL-Datenbank:
CREATE TABLE IF NOT EXISTS `coach` (
`ID` int(11) NOT NULL,
`Name` text NOT NULL,
`Age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `coach` (`ID`, `Name`, `Age`) VALUES
(1, 'Aaron', 39),
(2, 'John', 41),
(3, 'Macy', 44),
(4, 'Mitchelle', 37),
(5, 'Candice', 32);
CREATE TABLE IF NOT EXISTS `fee` (
`ID` int(11) NOT NULL,
`Coach_ID` int(11) NOT NULL,
`Trainee_ID` int(11) NOT NULL,
`Fee` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `fee` (`ID`, `Coach_ID`, `Trainee_ID`, `Fee`) VALUES
(11, 1, 7, 2400),
(12, 2, 6, 2000),
(13, 3, 6, 2000),
(14, 4, 8, 1243),
(15, 5, 8, 1275),
(16, 3, 9, 9010),
(17, 2, 8, 1900),
(18, 1, 7, 600),
(19, 2, 10, 1010),
(20, 5, 10, 2110);
CREATE TABLE IF NOT EXISTS `trainee` (
`ID` int(11) NOT NULL,
`Name` text NOT NULL,
`Age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `trainee` (`ID`, `Name`, `Age`) VALUES
(6, 'Abigail', 9),
(7, 'Jim', 12),
(8, 'Jack', 7),
(9, 'Maria', 14),
(10, 'Andy', 11);
Sie müssen die maximale (Gebühr) für jeden Trainer in der "Gebührentabelle" finden und die ID als eigenen Datensatz zurückgeben. Dann können Sie sich von diesem Datensatz aus innerlich verbinden, um beide Namen und die Gebühr mit dem richtigen Trainee in Verbindung zu bringen. – abraxascarab
Wie unterscheidet sich Ihre benötigte Ausgabe von der Ausgabe Ihrer Anfrage? Und welches RDBMS benutzt du? –
es identifiziert Jack als der Auszubildende mit der 2110 Gebühr anstelle von Andy – Danny