T
T
TurinTur2020-07-14 19:14:43
MySQL
TurinTur, 2020-07-14 19:14:43

How to check for the presence of records that meet the condition, and if the resulting number does not satisfy the condition, then make a new request?

Good afternoon.
The question is the following, I have a query to the database,
it works as it should, well, you need to continue it,
you need to understand if there are 4 records that satisfy this condition and if they are not, then make another query where there would be no duplicates of the query above and in the total score there were just 4 of them, the point is that I want to do all this in one request, if I split it into several, then I know how to do it, but I want to do it elegantly in one

SELECT f.*, 
GROUP_CONCAT(t.tagId) as tagId, 
GROUP_CONCAT(tt.img) as tagImg 
FROM tei_products as f 
JOIN tei_enrolments_tags t ON t.productId = f.id 
JOIN tei_tags tt ON tt.id = t.tagId 
JOIN tei_enrolments_tags t2 ON t2.productId = f.id 
JOIN tei_tags tt2 ON tt2.id = t2.tagId and tt.id = 2 
WHERE f.id != 38 AND f.active = 1 
GROUP BY f.id LIMIT 4

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-07-14
@Rsa97

Strange task. Why not immediately include this condition in the query, getting only the records you need.
Or, if it's too complicated, then remove LIMIT and get records from the database until the required number is reached.

K
Konstantin Tsvetkov, 2020-07-14
@tsklab

SELECT 0, … (первый запрос)
UNION
SELECT 1, … (второй запрос)
ORDER BY 1, … LIMIT 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question