h2jun
h2jun

Reputation: 13

How to select restrictive data on sql using like

SELECT * FROM table1 WHERE tagName LIKE '%ABC%' OR tagName LIKE '%DEF%';

I want to limit the number of data I get from each like. Is possible?

Upvotes: 1

Views: 51

Answers (2)

forpas
forpas

Reputation: 164069

You can do it with UNION ALL, but you have to nest each one of the statements as a subquery so that LIMIT can work, otherwise it's syntactically wrong:

SELECT * FROM (SELECT * FROM table1 WHERE tagName LIKE '%ABC%' LIMIT 10) t
UNION ALL
SELECT * FROM (SELECT * FROM table1 WHERE tagName LIKE '%DEF%' LIMIT 10) t

Note: LIMIT without ORDER BY is never a good idea.

Upvotes: 0

Ryan Tan
Ryan Tan

Reputation: 359

You could separate the queries by LIKE and specify the limit, finally UNION ALL

(SELECT * FROM table1 WHERE tagName LIKE '%ABC%' LIMIT 1)
UNION ALL
(SELECT * FROM table1 WHERE tagName LIKE '%DEF%' LIMIT 1);

Upvotes: 2

Related Questions