S
S
SomeEl172015-10-29 05:46:38
MySQL
SomeEl17, 2015-10-29 05:46:38

How to create nested queries so that the data from the outer one is used in the inner one?

The subject is strange, sorry.
I hope this request example is not so scary and clear.
SELECT a, ( SELECT COUNT(1) FROM (SELECT Bb FROM B WHERE Bb = a LIMIT 12) ) AS somename FROM A WHERE cond;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-10-29
@SomeEl17

3 levels of nesting, in my opinion - a firing squad. Better deal with JOIN. I would be happy to help, but there is no desire to understand this spherical horse in a vacuum.
UPD: Is that not possible?

SELECT a,Count(a) FROM A
INNER JOIN B ON B.b = A.a
WHERE ... 
GROUP BY a

W
wol_fi, 2015-10-29
@wol_fi

SELECT A.a, ( SELECT COUNT(1) FROM (SELECT B.b FROM B WHERE B.b = A.a LIMIT 12) ) AS somename FROM A WHERE cond;

probably like this (not sure), but it seems to me that it will work faster if you join:
SELECT A.a, if (X.cnt >= 12, 12 , X.cnt) as somename 
FROM A 
INNER JOIN (SELECT B.b, count(*) as cnt FROM B GROUP BY B.b) as X
 ON A.a = X.b 
WHERE cond

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question