我正在尝试从另一个表中选择记录并对其进行计数,并检查计数是否大于0
表1 =正在获取记录的表
STRUCTURE:
id | username | sponsorid
表2 =试图获取计数并检查计数是否大于0的表
STRUCTURE:
id | receiver_id | amount
查询:
SELECT t1.id AS lev1, t2.id AS lev2, t3.id AS lev3, t4.id AS lev4, t5.id AS lev5,
t6.id AS lev6, t7.id AS lev7, t8.id AS lev8, t9.id AS lev9, t10.id AS lev10
(SELECT COUNT(*) FROM tbl_master_fund where receiver_id = 1) as entry_count
FROM tbl_users AS t1
LEFT JOIN tbl_users AS t2
ON t2.sponsorid = t1.id
LEFT JOIN tbl_users AS t3
ON t3.sponsorid = t2.id
LEFT JOIN tbl_users AS t4
ON t4.sponsorid = t3.id
LEFT JOIN tbl_users AS t5
ON t5.sponsorid = t4.id
LEFT JOIN tbl_users AS t6
ON t6.sponsorid = t5.id
LEFT JOIN tbl_users AS t7
ON t7.sponsorid = t6.id
LEFT JOIN tbl_users AS t8
ON t8.sponsorid = t7.id
LEFT JOIN tbl_users AS t9
ON t9.sponsorid = t8.id
LEFT JOIN tbl_users AS t10
ON t10.sponsorid = t9.id
LEFT JOIN tbl_users AS t11
ON t11.sponsorid = t10.id
WHERE t1.sponsorid = 1
HAVING entry_count > 0
错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT COUNT(*) FROM tbl_master_fund where receiver_id = 1) as entry_count FROM' at line 1 (SQL: SELECT t1.id AS lev1, t2.id AS lev2, t3.id AS lev3, t4.id AS lev4, t5.id AS lev5, t6.id AS lev6, t7.id AS lev7, t8.id AS lev8, t9.id AS lev9, t10.id AS lev10 (SELECT COUNT(*) FROM tbl_master_fund where receiver_id = 1) as entry_count FROM tbl_atest_unilevel AS t1 LEFT JOIN tbl_atest_unilevel AS t2 ON t2.sponsorid = t1.id LEFT JOIN tbl_atest_unilevel AS t3 ON t3.sponsorid = t2.id LEFT JOIN tbl_atest_unilevel AS t4 ON t4.sponsorid = t3.id LEFT JOIN tbl_atest_unilevel AS t5 ON t5.sponsorid = t4.id LEFT JOIN tbl_atest_unilevel AS t6 ON t6.sponsorid = t5.id LEFT JOIN tbl_atest_unilevel AS t7 ON t7.sponsorid = t6.id LEFT JOIN tbl_atest_unilevel AS t8 ON t8.sponsorid = t7.id LEFT JOIN tbl_atest_unilevel AS t9 ON t9.sponsorid = t8.id LEFT JOIN tbl_atest_unilevel AS t10 ON t10.sponsorid = t9.id LEFT JOIN tbl_atest_unilevel AS t11 ON t11.sponsorid = t10.id WHERE t1.sponsorid = 1 HAVING entry_count > 0 )
编辑:现在显示是这样的。但是我想检查每个级别上每个用户的数量
我要完成的工作仅是在表2中有记录的情况下获得用户
答案 0 :(得分:0)
您在lev10之后错过了一个逗号
正确的查询是
date people
04/12/61 1
05/05/61 1
07/21/61 1
08/06/61 2
答案 1 :(得分:0)
lev10之后缺少逗号
SELECT t1.id AS lev1, t2.id AS lev2, t3.id AS lev3, t4.id AS lev4, t5.id AS lev5, t6.id AS lev6, t7.id AS lev7, t8.id AS lev8, t9.id AS lev9, t10.id AS lev10, (SELECT COUNT(*) FROM tbl_master_fund where receiver_id = 1) as entry_count FROM tbl_users AS t1 LEFT JOIN tbl_users AS t2 ON t2.sponsorid = t1.id LEFT JOIN tbl_users AS t3 ON t3.sponsorid = t2.id LEFT JOIN tbl_users AS t4 ON t4.sponsorid = t3.id LEFT JOIN tbl_users AS t5 ON t5.sponsorid = t4.id LEFT JOIN tbl_users AS t6 ON t6.sponsorid = t5.id LEFT JOIN tbl_users AS t7 ON t7.sponsorid = t6.id LEFT JOIN tbl_users AS t8 ON t8.sponsorid = t7.id LEFT JOIN tbl_users AS t9 ON t9.sponsorid = t8.id LEFT JOIN tbl_users AS t10 ON t10.sponsorid = t9.id LEFT JOIN tbl_users AS t11 ON t11.sponsorid = t10.id WHERE t1.sponsorid = 1 HAVING entry_count > 0