mysql视图显示1242错误

时间:2014-09-25 22:22:37

标签: php mysql view mysql-error-1064

我是Views的新手。我创建了以下视图

DELIMITER $$

USE `My_db`$$

DROP VIEW IF EXISTS `view_affiliate_banner_info_earning_details`$$

CREATE ALGORITHM=UNDEFINED DEFINER=`user`@`%` SQL SECURITY DEFINER VIEW `view_affiliate_banner_info_earning_details` AS (
SELECT DISTINCT
  `m`.`member_id` AS `member_id`,
  (SELECT
     (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END)
   FROM `view_member_cash_back` `v_cb`
   WHERE (`v_cb`.`member_id` = `m`.`member_id`)) AS `my_kash_back`,
  (SELECT
     (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN `v_gk`.`status` ELSE CONVERT((CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) USING latin1) END)
   FROM `view_good_bonus` `v_gk`
   WHERE (`v_gk`.`cash_back_to` = `m`.`member_id`)) AS `my_community_kash`,
  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'pending'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'pending')))),2)) AS `pending`,
  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'available'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'available')))),2)) AS `available`
FROM ((`view_members` `m`
    LEFT JOIN `view_member_cash_back` `v_cb`
      ON ((`m`.`member_id` = `v_cb`.`member_id`)))
   LEFT JOIN `view_good_bonus` `v_gk`
     ON ((`m`.`member_id` = `v_gk`.`cash_back_to`)))
WHERE 1
ORDER BY `m`.`member_id`)$$

DELIMITER ;

在创建时它没有给我任何错误,但是当我想在这个视图中看到数据时,它会给我以下错误 子查询返回超过1行

更新

我改变了这样的查询,但仍然是相同的

DELIMITER $$

USE `my_db`$$

DROP VIEW IF EXISTS `view_affiliate_banner_info_earning_details`$$

CREATE ALGORITHM=UNDEFINED DEFINER=`user`@`%` SQL SECURITY DEFINER VIEW `view_affiliate_banner_info_earning_details` AS (
SELECT DISTINCT
  `m`.`member_id` AS `member_id`,
  (SELECT
     (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END)
   FROM `view_member_cash_back` `v_cb`
   WHERE (`v_cb`.`member_id` = `m`.`member_id`) LIMIT 1) AS `my_kash_back`,
  (SELECT
     (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN `v_gk`.`status` ELSE CONVERT((CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) USING latin1) END)
   FROM `view_good_bonus` `v_gk`
   WHERE (`v_gk`.`cash_back_to` = `m`.`member_id`) LIMIT 1) AS `my_community_kash`,
  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'pending'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'pending')))),2)) AS `pending`,
  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'available'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'available')))),2)) AS `available`
FROM ((`view_members` `m`
    LEFT JOIN `view_member_cash_back` `v_cb`
      ON ((`m`.`member_id` = `v_cb`.`member_id`)))
   LEFT JOIN `view_good_bonus` `v_gk`
     ON ((`m`.`member_id` = `v_gk`.`cash_back_to`)))
WHERE 1
ORDER BY `m`.`member_id`)$$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

评论太长了。

问题是什么? select子句中有两个子查询。其中一个是返回多行。快速修复方法是将limit 1添加到两个子查询中。

事实上,我们可以缩小范围。第一个子查询是聚合子查询,因此它只返回一行。问题是第二个问题:

  (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN `v_gk`.`status` ELSE CONVERT((CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) USING latin1) END)
   FROM `view_good_bonus` `v_gk`
   WHERE (`v_gk`.`cash_back_to` = `m`.`member_id`)
  ) AS `my_community_kash`,

您可以在limit 1之后加where进行快速而肮脏的修复。您可以通过在select子句中放置聚合函数来解决此问题(与第一个查询一样)。或者,您可以根据需要弄清楚为什么数据不会像您认为的那样运行并修复数据或查询或两者。

编辑:

当我第一次回答时,我错过了两个以上的相关子查询而不是其他子句在这些表达式中(不方便地滚动页面):

  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'pending'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'pending')))),2)) AS `pending`,
  CONCAT(TRUNCATE(((SELECT (CASE WHEN ISNULL(ROUND(SUM(`v_cb`.`kash_back`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_cb`.`kash_back`),2),2) END) FROM `view_member_cash_back` `v_cb` WHERE ((`v_cb`.`member_id` = `m`.`member_id`) AND (`v_cb`.`status` = 'available'))) + (SELECT (CASE WHEN (`v_gk`.`status` = 'upgrade') THEN '0.00' ELSE (CASE WHEN ISNULL(ROUND(SUM(`v_gk`.`good_bonus`),0)) THEN '0.00' ELSE TRUNCATE(ROUND(SUM(`v_gk`.`good_bonus`),2),2) END) END) FROM `view_good_bonus` `v_gk` WHERE ((`v_gk`.`cash_back_to` = `m`.`member_id`) AND (`v_gk`.`status` = 'available')))),2)) AS `available`

其中一个也可能返回多行,因此在所有这些中包含limit 1子句应该可以解决问题。