1

The first SELECT for example returns 57 and 61. I want the second sql command to use returned rows of first SELECT not like what I have put here (manually 57 and 61). Finally calculate sum of these results. (9 is the result of course)

1. SELECT id FROM permissions WHERE title = 'samplezip'   
2. SELECT count(id) FROM accesslogs WHERE urlid=57 AND status='d' //result 7  
3. SELECT count(id) FROM accesslogs WHERE urlid=61 AND status='d' //result 2

2 Answers 2

2

You can do this all in one i think:

select
    count(l.id)
from
    permissions as p
    inner join accesslogs as l
        on p.id = l.urlid
where 
    p.title = 'samplezip'
    and status ='d'
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, Now it gives two rows. not calculated sum! Before group by it was correct.
@MessiMeysam i have rolled back the changes made in gordatron's answer, just also ignore p.title, p.id from select part these values will be returned in in-deterministic order
1

Use IN:

SELECT count(*)
FROM accesslogs 
WHERE status = 'd'
  AND urlid IN (SELECT id FROM permissions WHERE title = 'samplezip');

For a result row per ID plus a total use GROUP BY WITH ROLLUP:

SELECT urlid, count(*)
FROM accesslogs 
WHERE status = 'd'
  AND urlid IN (SELECT id FROM permissions WHERE title = 'samplezip')
GROUP BY urlid WITH ROLLUP;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.