2

I would like to extract 100 names from table t1 and t2 where id = 'value', in such a way so that if table t1 hasn't 100 names, I will then go and take the remaining names from table t2.

The first part of the query would be (for table t1)

SELECT distinct name, id
FROM t1 WHERE id = 'value' 
ORDER BY id 
LIMIT 100;
1

1 Answer 1

1

You should be able to do this by using a union to join the two queries together. This assumes that Origin is a value that signifies which table is being queried so that results from t1 take precedence.

SELECT 1 as origin, name, id

FROM t1 WHERE id = 'value' 

union

SELECT 2 as origin, name, id

FROM t2 WHERE id = 'value' 

ORDER BY origin, id 

LIMIT 100
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.