找回密码
 立即注册

QQ登录

只需一步,快速开始

哇哈哈哈

注册会员

16

主题

68

帖子

194

积分

注册会员

积分
194

[已处理] 来个MySQL大佬

哇哈哈哈
注册会员   /  发表于:2024-6-14 16:04  /   查看:1332  /  回复:8
50金币

SELECT  A.customer_name,A.goods_name,A.code,A.wholesale_number,A.wholesale_price,A.wholesale_amount,B.purchase_quantity,C.purchase_quantity1,D.purchase_quantity2
FROM
    (SELECT T1.customer_name,T1.goods_name,T1.code,SUM(T1.wholesale_number) AS wholesale_number,SUM(T1.wholesale_price) AS wholesale_price,SUM(T1.wholesale_amount) AS wholesale_amount
        FROM 设备清单两表相加结果 T1
            GROUP BY T1.customer_name,T1.goods_name,T1.code) A
    LEFT JOIN
         (SELECT T2.customer_name,T2.goods_name,T2.code,SUM(T2.purchase_quantity) AS purchase_quantity FROM tb_purchase_request_hua T2
            GROUP BY T2.customer_name,T2.goods_name,T2.code) B
               ON A.customer_name=B.customer_name AND A.goods_name=B.goods_name AND A.code=B.code
    LEFT JOIN
      (SELECT T3.customer_name,T3.goods_name,T3.code,SUM(T3.purchase_quantity1) AS purchase_quantity1 FROM tb_purchase_receipt_hua T3
            GROUP BY T3.customer_name,T3.goods_name,T3.code) C
              ON A.customer_name=C.customer_name AND A.goods_name=C.goods_name AND A.code=C.code
    LEFT JOIN
      (SELECT T4.customer_name,T4.goods_name,T4.code,SUM(T4.purchase_quantity2) AS purchase_quantity2 FROM tb_installation_checklist_hua T4
            GROUP BY T4.customer_name,T4.goods_name,T4.code) D
              ON A.customer_name=D.customer_name AND A.goods_name=D.goods_name AND A.code=D.code;



报错View's SELECT contains a subquery in the FROM clause
设备清单两表相加结果  这个主表也是视图来了~可以解救下吗,大佬,,运行是可以正常,但建议视图就报错了

最佳答案

查看完整内容

CREATE view view_A AS (SELECT T1.customer_name,T1.goods_name,T1.code,SUM(T1.wholesale_number) AS wholesale_number,SUM(T1.wholesale_price) AS wholesale_price,SUM(T1.wholesale_amount) AS wholesale_amount FROM 设备清单两表相加结果 T1 GROUP BY T1.customer_name,T1.goods_name,T1.code); CREATE view view_B AS (SELECT T2.customer_name,T2.goods_name,T2.code,SUM(T2.purchase_quantity) AS purchase_q ...

8 个回复

倒序浏览
最佳答案
最佳答案
凄美地
初级会员   /  发表于:2024-6-14 16:04:03
来自 5#
哇哈哈哈 发表于 2024-6-14 16:26
来个完整代码。大佬,小白一个

CREATE view view_A AS
(SELECT T1.customer_name,T1.goods_name,T1.code,SUM(T1.wholesale_number) AS wholesale_number,SUM(T1.wholesale_price) AS wholesale_price,SUM(T1.wholesale_amount) AS wholesale_amount
FROM 设备清单两表相加结果 T1
GROUP BY T1.customer_name,T1.goods_name,T1.code);

CREATE view view_B AS
(SELECT T2.customer_name,T2.goods_name,T2.code,SUM(T2.purchase_quantity) AS purchase_quantity
FROM tb_purchase_request_hua T2
GROUP BY T2.customer_name,T2.goods_name,T2.code);

CREATE view view_C AS
(SELECT T3.customer_name,T3.goods_name,T3.code,SUM(T3.purchase_quantity1) AS purchase_quantity1
FROM tb_purchase_receipt_hua T3
GROUP BY T3.customer_name,T3.goods_name,T3.code);

CREATE view view_D AS
(SELECT T4.customer_name,T4.goods_name,T4.code,SUM(T4.purchase_quantity2) AS purchase_quantity2
FROM tb_installation_checklist_hua T4
GROUP BY T4.customer_name,T4.goods_name,T4.code);

CREATE VIEW my_view AS
SELECT  A.customer_name,A.goods_name,A.code,A.wholesale_number,A.wholesale_price,A.wholesale_amount,B.purchase_quantity,C.purchase_quantity1,D.purchase_quantity2
FROM view_A A
LEFT JOIN view_B B ON A.customer_name=B.customer_name AND A.goods_name=B.goods_name AND A.code=B.code
LEFT JOIN view_C C ON A.customer_name=C.customer_name AND A.goods_name=C.goods_name AND A.code=C.code
LEFT JOIN view_D D ON A.customer_name=D.customer_name AND A.goods_name=D.goods_name AND A.code=D.code;
回复 使用道具 举报
Syl悬赏达人认证
银牌会员   /  发表于:2024-6-14 16:09:12
2#
sql语法都差不多,发个demo或者 建表语句都行
回复 使用道具 举报
凄美地
初级会员   /  发表于:2024-6-14 16:18:04
3#
mysql好像有的版本不支持视图子查询,可以把ABCD子查询先写成视图,再查这四个视图
回复 使用道具 举报
哇哈哈哈
注册会员   /  发表于:2024-6-14 16:26:10
4#
凄美地 发表于 2024-6-14 16:18
mysql好像有的版本不支持视图子查询,可以把ABCD子查询先写成视图,再查这四个视图

来个完整代码。大佬,小白一个
回复 使用道具 举报
豪~豪悬赏达人认证 活字格认证
银牌会员   /  发表于:2024-6-17 08:56:38
6#
  1. SQL 语句中的错误提示 "View's SELECT contains a subquery in the FROM clause" 通常指的是在视图的 SELECT 语句中使用了子查询,这在某些数据库系统中是不允许的,或者有特定的限制。

  2. 在你提供的 SQL 语句中,你试图从一个视图 `设备清单两表相加结果` 中选择数据,并且这个视图本身可能也是由一个子查询构成的。这可能是导致错误的原因。

  3. 解决这个问题的方法之一是将视图的定义改为一个临时表或者直接在查询中使用子查询,而不是创建视图。以下是将你的视图转换为子查询的示例:

  4. ```sql
  5. SELECT
  6.     A.customer_name,
  7.     A.goods_name,
  8.     A.code,
  9.     A.wholesale_number,
  10.     A.wholesale_price,
  11.     A.wholesale_amount,
  12.     B.purchase_quantity,
  13.     C.purchase_quantity1,
  14.     D.purchase_quantity2
  15. FROM
  16.     (SELECT
  17.         T1.customer_name,
  18.         T1.goods_name,
  19.         T1.code,
  20.         SUM(T1.wholesale_number) AS wholesale_number,
  21.         SUM(T1.wholesale_price) AS wholesale_price,
  22.         SUM(T1.wholesale_amount) AS wholesale_amount
  23.      FROM
  24.         (你的原始查询或表名) AS T1
  25.      GROUP BY
  26.         T1.customer_name,
  27.         T1.goods_name,
  28.         T1.code) A
  29. LEFT JOIN
  30.     (SELECT
  31.         T2.customer_name,
  32.         T2.goods_name,
  33.         T2.code,
  34.         SUM(T2.purchase_quantity) AS purchase_quantity
  35.      FROM
  36.         tb_purchase_request_hua AS T2
  37.      GROUP BY
  38.         T2.customer_name,
  39.         T2.goods_name,
  40.         T2.code) B
  41.     ON A.customer_name = B.customer_name
  42.    AND A.goods_name = B.goods_name
  43.    AND A.code = B.code
  44. LEFT JOIN
  45.     (SELECT
  46.         T3.customer_name,
  47.         T3.goods_name,
  48.         T3.code,
  49.         SUM(T3.purchase_quantity1) AS purchase_quantity1
  50.      FROM
  51.         tb_purchase_receipt_hua AS T3
  52.      GROUP BY
  53.         T3.customer_name,
  54.         T3.goods_name,
  55.         T3.code) C
  56.     ON A.customer_name = C.customer_name
  57.    AND A.goods_name = C.goods_name
  58.    AND A.code = C.code
  59. LEFT JOIN
  60.     (SELECT
  61.         T4.customer_name,
  62.         T4.goods_name,
  63.         T4.code,
  64.         SUM(T4.purchase_quantity2) AS purchase_quantity2
  65.      FROM
  66.         tb_installation_checklist_hua AS T4
  67.      GROUP BY
  68.         T4.customer_name,
  69.         T4.goods_name,
  70.         T4.code) D
  71.     ON A.customer_name = D.customer_name
  72.    AND A.goods_name = D.goods_name
  73.    AND A.code = D.code;
  74. ```

  75. 请注意,我将 `设备清单两表相加结果` 替换成了 `(你的原始查询或表名)`,你需要将其替换为实际的表名或者原始查询。

  76. 如果问题依旧存在,可能需要检查数据库的具体错误信息和文档,以确定是否是数据库系统的限制或者需要其他特定的解决方案。
复制代码
回复 使用道具 举报
哇哈哈哈
注册会员   /  发表于:2024-6-17 09:01:11
7#
凄美地 发表于 2024-6-14 16:34
CREATE view view_A AS
(SELECT T1.customer_name,T1.goods_name,T1.code,SUM(T1.wholesale_number) AS  ...

多谢大佬!可以了~~~~
回复 使用道具 举报
哇哈哈哈
注册会员   /  发表于:2024-6-17 09:53:41
8#
豪~豪 发表于 2024-6-17 08:56

按上面大佬可以了,谢谢大佬
回复 使用道具 举报
Lay.Li悬赏达人认证 活字格认证
超级版主   /  发表于:2024-6-17 10:59:19
9#
感谢各位大佬的支持~
后边有问题呢欢迎您继续发帖交流
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部