Is it possible to accomplish this task with a single MySQL query?
URL table. Fields { id , URL }
1, www.mysite.kom
2, mysite.kom
3, anothersite.kom
Table logs . Fields { id , url_id , group_type - a number in the range 1..10}
1, 1, 4
2, 1, 4
3, 2, 5
4, 2, 5
5, 3, 9
The result of the query in this example should be: 1 (mysite.com and www.mysite.com = 1)
TARGET:
You must consider all the different URLs recorded in the logs table, but with several conditions:
1) Called with www. and without it mysite.kom and www.mysite.kom . 1 (not 2) is considered.
2) Have group_type in the range 4..6
3) Now any of these URLs with group_type 4..6 that appear in the list of those with group_type below 4 should be ignored, not calculated at all.
SQL code:
SELECT COUNT(DISTINCT TRIM(LEADING 'www.' FROM b.url)) FROM logs a INNER JOIN urls b ON a.url_id = b.id WHERE (group_type BETWEEN 4 AND 6)
If my sql query is correct, can it be optimized (to look more compact)?