phpmyadmin Error =>#1242 -子查询返回1行以上

ne5o7dgx  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(101)

我正在从orders表中选择订单数据,同时还需要包含特定单词的订单产品计数
帐户充值200,
账户充值500,
账户充值1000,
订单包含多个产品意味着一个订单同时包含(帐户TopUp 200、帐户TopUp 500)和其他产品
我为此做了一个子查询

(
                    SELECT COUNT(orderprodid)
                    FROM order_products
                    WHERE orderorderid=orderid AND ordprodname like '%Account TopUp%'
                ) AS  orderproductcount

但它给出错误:#1242 -子查询返回1行以上
完整查询为:

SELECT o.*,
                    (
                        SELECT COUNT(messageid)
                        FROM order_messages
                        WHERE messageorderid=orderid
                    ) AS nummessages,
                    (
                        SELECT COUNT(messageid)
                        FROM order_messages
                        WHERE messageorderid=orderid AND messagestatus != 'read'
                    ) AS numunreadmessages,
                    (
                        SELECT COUNT(messageid)
                        FROM order_messages
                        WHERE messageorderid=orderid AND messagefrom='customer' AND messagestatus='unread'
                    ) AS newmessages,
                    (
                        SELECT orderproapi
                        FROM order_products
                        WHERE orderorderid=orderid
                    ) AS  orderproapi,
                    (
                        SELECT COUNT(orderprodid)
                        FROM order_products
                        WHERE orderorderid=orderid AND ordprodname like '%Account TopUp%'
                    ) AS  orderproductcount
                FROM orders o
                LEFT JOIN customers c ON (o.ordcustid=c.customerid)
                LEFT JOIN order_status s ON (s.statusid=o.ordstatus)
wtzytmuj

wtzytmuj1#

您在问题中突出显示的子查询实际上没有任何问题,也不会导致您看到的错误消息。但此子查询将:

(
    SELECT orderproapi
    FROM order_products
    WHERE orderorderid = orderid
) AS orderproapi,

此子查询很可能会返回多个记录/值,因此出现在SELECT子句中没有意义。请解决此问题,您的查询应该可以正常工作。

相关问题