MySQL问题|子查询返回多行

时间:2016-02-08 10:33:10

标签: mysql

$SQL->query('SELECT `p`.`name` AS  `name`,
  (SELECT `k`.`value` FROM `player_storage` k LEFT JOIN `players` p ON `k`.`player_id` = `p`.`id` WHERE `k`.`key` = 11000) AS value
   FROM `player_storage` k, `players` p
   WHERE `k`.`player_id` = `p`.`id`
   GROUP BY  `name` 
   ORDER BY CAST(`value` AS SIGNED) 
   DESC LIMIT 0 , 30
   ;')

错误:子查询返回的行数超过1行

由于我对mysql一无所知,有人可以帮助我吗?

我尝试从同一列中获取2个值 key = 11000 key = 11001

2 个答案:

答案 0 :(得分:1)

由于错误清楚地表明您的子查询在主查询中针对单行返回2行,因此如何才能获得错误。

您可以通过以下查询获得结果 -

SELECT `p`.`name` AS  `name`,
 `p`.`skull` AS `skull`,
  (SELECT `k`.`value` FROM `player_storage` k LEFT JOIN `players` p ON `k`.`player_id` = `p`.`id` WHERE `k`.`key` = 11000 LIMIT 1) AS VALUE
   FROM `player_storage` k, `players` p
   WHERE `k`.`player_id` = `p`.`id`
   GROUP BY  `name` 
   ORDER BY CAST(`value` AS SIGNED) 
   DESC LIMIT 0 , 30

注意:不确定您是否需要相同的结果或其他内容,但它会帮助您解决问题。

答案 1 :(得分:1)

试试这个

select `p`.`name` AS  `name`,
  (
    select `k`.`value` from `player_Storage` k
    where `k`.`key` =11000 and `k`.`player_id` not in (select `p` .`id` from `player` p)
  ) AS value
   FROM `player_storage` k, `players` p
   WHERE `k`.`player_id` = `p`.`id`
   GROUP BY  `name` 
   ORDER BY CAST(`value` AS SIGNED) 
   DESC LIMIT 0 , 30;