Search code examples
phpmysqldatabaseleft-joincardinality

Mysql Error code: 1242 Subquery returns more than 1 row


Everything seemed to be working fine, then all of a sudden I am getting this error on my front-end:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row' in /var/www/html/system/home.php:78 Stack trace: #0 /var/www/html/system/home.php(78): PDOStatement->execute() #1 {main} thrown in /var/www/html/system/home.php on line 78

Under Workbench every VIEW I had created earlier is giving out this error: Mysql Error code: 1242 Subquery returns more than 1 row.

Here is an example of one code that's giving the error.

SELECT 
    `teams`.`id` AS `id`,
    `teams`.`name` AS `team_name`,
    `teams_and_captains_view`.`captain` AS `captain`,
    IF((`teams`.`gender` = 'M'),
        'Male',
        'Female') AS `gender`,
    (SELECT 
            `categories`.`name`
        FROM
            `categories`
        WHERE
            (`categories`.`id` = `teams`.`category_id`)) AS `category`,
    `teams`.`category_id` AS `category_id`,
    `teams`.`slogan` AS `slogan`,
    `teams`.`location` AS `location`,
    `teams`.`description` AS `description`,
    `teams`.`phone` AS `phone`,
    `teams`.`email` AS `email`,
    `teams`.`tournament_id` AS `tournament_id`,
    (SELECT 
            COUNT(`players`.`id`)
        FROM
            `players`
        WHERE
            (`players`.`team_id` = `teams`.`id`)) AS `players`,
    `teams_and_captains_view`.`captain_id` AS `captain_id`
FROM
    (`teams`
    LEFT JOIN `teams_and_captains_view` ON ((`teams`.`id` = `teams_and_captains_view`.`id`)))

It seems to work when I use LIMIT, but I can't use LIMIT.

Thank you in advance


Solution

  • Error code 1242 will be returned when Your subquery returns more than one row for comparison.

    Please check if you have inserted any duplicate data by mistake.

    http://dev.mysql.com/doc/refman/5.7/en/subquery-errors.html