我试图从我们的订单表中检索客户和发票 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;