简体   繁体   English

错误 => #1242 - 子查询返回多于 1 行

[英]Error => #1242 - Subquery returns more than 1 row

I am selecting order data from my orders table with that i also need the order product's count which contain specific word我正在从我的订单表中选择订单数据,我还需要包含特定单词的订单产品计数

Account TopUp 200,账户充值200,
Account TopUp 500,账户充值500,
Account TopUp 1000,账户充值1000,

The order contain multipal product means one order contain both(Account TopUp 200, Account TopUp 500) and other products订单包含多个产品意味着一个订单包含(帐户充值 200、帐户充值 500)和其他产品

I made subquery for this我为此做了子查询

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

But it gives Error: #1242 - Subquery returns more than 1 row但它给出了错误:#1242 - 子查询返回超过 1 行

Full query is:完整的查询是:

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)

The subquery you highlighted in your question actually doesn't have any problem with it and won't cause the error message you are seeing.您在问题中突出显示的子查询实际上没有任何问题,并且不会导致您看到的错误消息。 But this subquery will:但是这个子查询将:

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

This subquery will most likely return multiple records/values, and therefore makes no sense appearing in the SELECT clause.此子查询很可能会返回多个记录/值,因此出现在SELECT子句中没有任何意义。 Fix this problem and your query should work.解决此问题,您的查询应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM