0
Good to start I will post the SQL I rode here:
SELECT
a.*,
b.name AS currentClubName,
c.name AS transferorClubName,
d.name AS contactName,
d.phone AS contactPhone,
d.phoneApp AS contactPhoneApp,
d.email AS contactEmail,
d.note AS contactNote,
e.endDate
FROM
athlete a
LEFT JOIN clubs b ON a.currentClubCode = b.code
LEFT JOIN clubs c ON a.transferorClubCode = c.code
LEFT JOIN contacts d ON a.contactCode = d.code
LEFT JOIN negotiation e ON e.athleteCode = a.code
ORDER BY
a.name;
Keeping in mind that the table athlete
has only one a.currentClubCode
,a.transferorClubCode
and a a.contactCode
, the answer will not suffer duplicity.
But in the case of the table negotiation
are recorded several negotiations for an athlete and I need to recover only the e.endDate
of the longest date, that is to say only a result.
In the current way I am having a duplicity, if a player has 3 record in the table negotiation
he is listed 3 times in select.
How to solve this? I tried to use the LEFT JOIN
as there may be no record of trading and therefore field e.endDate
must be null.
Note: I am using version 8 of mysql.
Follows the structure of the table negotiation
:
CREATE TABLE IF NOT EXISTS `negotiation` (
`code` INT AUTO_INCREMENT,
`athleteCode` int(11) NOT NULL,
`date` date DEFAULT NULL,
`endDate` date NOT NULL,
`note` text NOT NULL,
`link` text DEFAULT NULL,
PRIMARY KEY (`code`),
FOREIGN KEY (athleteCode) REFERENCES athlete(code) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
__________ Edit _____________
I got the result I wanted that way, but I don’t know if it’s the best way to do it:
SELECT
a.*,
b.name AS currentClubName,
c.name AS transferorClubName,
d.name AS contactName,
d.phone AS contactPhone,
d.phoneApp AS contactPhoneApp,
d.email AS contactEmail,
d.note AS contactNote,
(SELECT endDate FROM negotiation WHERE athleteCode = a.code ORDER BY endDate DESC LIMIT 1) AS endDate
FROM
athlete a
LEFT JOIN clubs b ON a.currentClubCode = b.code
LEFT JOIN clubs c ON a.transferorClubCode = c.code
LEFT JOIN contacts d ON a.contactCode = d.code
ORDER BY
a.name;
I suggest you perform a subselect on the last left Join bringing the codes along with max(endDate) to bring the latest code, as you did not put the structure of this table I can not assemble the code for you :)
– Heitor Scalabrini
I will update the question with the table structure
negotiation
– Hugo Borges