0

我试图从我们的订单表中检索客户和发票 ID 的列表,其中客户只有一个订单。下面的 SQL 返回零条记录,应该有几十条。如果我将有效的用户 ID 代入其中,子查询本身似乎可以正常工作。我一定是做错了什么,有什么帮助吗?

SELECT tblclients.id AS clientid, tblinvoices.id AS invoiceid
            FROM tblorders
            join tblinvoices ON tblorders.invoiceid = tblinvoices.id
            join tblclients ON tblorders.userid = tblclients.id
            WHERE (SELECT COUNT(*) FROM tblorders WHERE userid = tblorders.userid) = 1;
4

1 回答 1

1

我认为您的别名有问题。尝试添加 T1 别名:

SELECT tblclients.id AS clientid, tblinvoices.id AS invoiceid
            FROM tblorders T1
            join tblinvoices ON T1.invoiceid = tblinvoices.id
            join tblclients ON T1.userid = tblclients.id
            WHERE (SELECT COUNT(*) FROM tblorders WHERE userid = T1.userid) = 1;
于 2012-08-03T16:24:51.233 回答