using a variable in a SUBQUERY is returning more than one row

37 views Asked by At

I'm trying to reuse the @FleetNumber variable to get the Truck's Trailer registration Number through the fleet number <fleetnumber>T eg.393T. The Trailer has the fleet number of the <fleetnumber>L eg. 393L. This following query works if the main query returns a singular record, but I need it to return multiple records.

When removing the where clause I get the error Subquery returns more than 1 row. The query needs to get the trailer Registration for every fleet number that is in the HANDOVER table.

SELECT
    h.TRANSACTION_NUMBER,
    a.RECORD_ID AS `A_ASSET_ID`

    @FleetNumber := a.FLEET_NUMBER,

    (
        SELECT a1.REGISTRATION_NUMBER
        FROM newtodme_newton_web_portal.ASSETS a1
        WHERE a1.FLEET_NUMBER = REPLACE(@FleetNumber, 'T', 'L')
    ) AS `TRAILER_REG`

FROM newtodme_newton_hand_over.HANDOVERS h
LEFT JOIN newtodme_newton_web_portal.ASSETS a ON h.ASSET_ID = a.RECORD_ID;
/*WHERE h.TRANSACTION_NUMBER = 4000*/
0

There are 0 answers