我试图在联接表
中使用like来计算order_registration.statusSELECT dstr_operator_master.*,
count(order_registration.status like'%enquiry%') AS order_status,
count(order_registration.pname) AS destiList
FROM dstr_operator_master
left JOIN order_registration
ON FIND_IN_SET( order_registration.user_id , dstr_operator_master.u_id)
WHERE dstr_operator_master.status = '1'
and dstr_operator_master.type ='distributor'
GROUP BY dstr_operator_master.auto_id
答案 0 :(得分:1)
试试这个:
SELECT d.*,
SUM(CASE WHEN o.status LIKE '%enquiry%' THEN 1 ELSE 0 END) AS order_status,
COUNT(o.pname) AS destiList
FROM dstr_operator_master d
LEFT JOIN order_registration o ON FIND_IN_SET(o.user_id, d.u_id)
WHERE d.status = '1' AND d.type = 'distributor'
GROUP BY d.auto_id;
答案 1 :(得分:0)
您的计数可能会返回相同的值,因为COUNT()
计算非NULL
值的数量。对于第一个,请使用SUM()
代替COUNT()
:
SELECT dstr_operator_master.*,
SUM(order_registration.status LIKE'%enquiry%') AS order_status,
Count(order_registration.pname) AS destiList
. . .
MySQL将布尔值视为数字上下文中的数字,其中true为1,false为0.这就是SUM()
有效的原因。