2

第一个查询:

SELECT COUNT(*)
FROM `test` t ,`user_test` ut,`user` u 
WHERE test_type_id = 1
AND t.created_on >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.created_on < CURDATE()
AND t.id = ut.test_id
AND u.id = ut.user_id
AND u.user_account_type_id != X 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...'  
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 

第二个查询:

SELECT COALESCE(SUM(IF(tt.type = 0, 1, 0)), 0) aaa, COALESCE(SUM(IF(tt.type = 1, 1, 0)), 0) bbb 
FROM transaction_test tt ,USER u
WHERE tt.created_on >= DATE_SUB(CURDATE(), INTERVAL 10000 DAY) AND tt.created_on < CURDATE()
AND u.id = tt.user_id
AND u.user_account_type_id != X
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...'  
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 

必须从一个选择中获得结果我试试这个

SELECT COALESCE(SUM(IF(tt.type = 0, 1, 0)), 0) Email, COALESCE(SUM(IF(tt.type = 1, 1, 0)), 0) SMS ,(
SELECT COUNT(*)
FROM `test` t ,`user_test` ut,`user` u 
WHERE test_type_id = 1
AND t.created_on >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.created_on < CURDATE()
AND t.id = ut.test_id
AND u.id = ut.user_id
AND u.user_account_type_id != X 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...'  
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
)
FROM transaction_test tt ,USER u
WHERE tt.created_on >= DATE_SUB(CURDATE(), INTERVAL 10000 DAY) AND tt.created_on < CURDATE()
AND u.id = tt.user_id
AND u.user_account_type_id != X
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...'  
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 

我不满意使用两次

AND u.user_account_type_id != X
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...'  
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' 

这部分任何机构都可以建议如何避免这种情况?提前致谢

4

1 回答 1

0

Give it a try i have joined your both query because in queries you have the same conditions and i have used the INNER JOIN to sure that both tables have the data if your concerned to get data from the right table or left table change it accordingly to the RIGHT JOIN or LEFT JOIN

SELECT COUNT(*) ,COALESCE(SUM(IF(tt.type = 0, 1, 0)), 0) aaa, COALESCE(SUM(IF(tt.type = 1, 1, 0)), 0) bbb 
FROM `test` t ,
INNER JOIN `user_test` ut ON (t.id = ut.test_id)
INNER JOIN `user` u  ON (u.id = ut.user_id)
INNER JOIN `transaction_test` tt  ON (u.id = tt.user_id)
WHERE test_type_id = 1
AND t.created_on >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.created_on < CURDATE()
AND u.user_account_type_id != X 
AND u.usr NOT LIKE '%...' AND u.usr NOT LIKE '%...' AND u.usr NOT LIKE '%...' 
AND u.usr NOT LIKE '%...' AND u.usr NOT LIKE '%...' AND u.usr NOT LIKE '%...' 

Hope it works fine

于 2013-07-03T11:55:36.310 回答