Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的SQL查询处于非工作状态_Php_Mysql_Sql - Fatal编程技术网

Php 我的SQL查询处于非工作状态

Php 我的SQL查询处于非工作状态,php,mysql,sql,Php,Mysql,Sql,我的SQL查询处于非工作状态。不是u.paymentmethod=3请告诉我如何查询这个完美的。提前谢谢 SELECT (SELECT COUNT(u.`refered`) FROM user u WHERE u.refered = user.user_id AND u.paymentmethod = 2 and NOT u.paymentmethod=3 ) as ref, user_id, payme

我的SQL查询处于非工作状态。不是u.paymentmethod=3请告诉我如何查询这个完美的。提前谢谢

SELECT (SELECT COUNT(u.`refered`) 
         FROM user u 
         WHERE u.refered = user.user_id 
         AND  u.paymentmethod = 2 and NOT u.paymentmethod=3  ) as ref,
       user_id,
       paymentmethod 
from user  
HAVING ref < 6

您使用的语法错误,无法更改查询,如下所示:

SELECT (SELECT COUNT(u.`refered`) 
         FROM user u 
         WHERE u.refered = user.user_id 
         AND  u.paymentmethod = 2 and u.paymentmethod != 3  ) as ref,
       user_id,
       paymentmethod 
from user  
HAVING ref < 6 and user.paymentmethod = 2

这是一个地狱般的方式来写它,以避免自我加入

SELECT COUNT(u.`refered`) as ref
       a.user_id,
       a.paymentmethod 
from user a
left join user u
  on u.refered = a.user_id
  and u.paymentmethod = 2 
  and u.paymentmethod <> 3 -- not needed, but might as well include it
group by user_id, paymentmethod
HAVING ref < 6

您需要更改的只是条件。如下面的代码

SELECT (SELECT COUNT(u.`refered`) 
         FROM user u 
         WHERE u.refered = user.user_id 
         AND  u.paymentmethod = 2 AND u.paymentmethod<>3  ) as ref,
       user_id,
       paymentmethod 
from user  
HAVING ref < 6

如果不让我们知道您想要实现什么以及您的示例数据是什么,将很难提供帮助,请发布这些信息。不要使用NOT use或!=symbol i.e u.paymentmethod3我想你不需要那个not条件,你已经比较过u.paymentmethod=2,它本身不允许u.paymentmethod=3。我想得到用户ID的谁是paymentmethod 2而不是3,但是这个查询也会显示用户ID paymentmethod 3。我不想要。当然,你会在结果中得到所有付款,因为主查询没有WHERE子句。但是,子查询中的计数将不包括它们。删除和u.paymentmethod!=3因为u.paymentmethod=2表示它将是2,而不是任何其他值。感谢您伟大的回答@B。对不起,它的输出相同。我想要获取paymentmethod=2而不是3的用户ID。谢谢你能出示你的样本数据吗?因为如果paymentmethod=2,那么它就不会是3Hello@B.Desai。请检查此付款方法3中的此链接也显示,但我只想显示paymentmethod 2而不是3。谢谢,太好了,兄弟,谢谢,这很有魅力。你能告诉我哪里错了吗??
SELECT (SELECT COUNT(u.`refered`) 
         FROM user u 
         WHERE u.refered = user.user_id 
         AND  u.paymentmethod = 2 AND u.paymentmethod<>3  ) as ref,
       user_id,
       paymentmethod 
from user  
HAVING ref < 6