4

Help create a search condition

SELECT *
FROM mlt_adr_city
WHERE name LIKE "Text%" 
AND region_id = 59
AND id <> 0 
IF (name = name, LIMIT 1, LIMIT 5)

Value field name can coincidence.
If the value is the same output a single line, or five.
Sorry i am bad english

[copied from comments:]

If the request without the condition, the names in which the records are repeated. For example WHERE name LIKE "City1" with the same name will return five rows but id they will be different.

But if there is no match then display five records.

Example Search LIKE "City1%" display records three City1, City1, City1.

Example 2. Search LIKE "City2%" display records three City2, City2, City2_en, City2_rect, City2_les.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Vayas
  • 107
  • 3
  • 11
  • 2
    Maybe you can give an example of the types of names you're matching, and the type of output you want. – tadman Nov 30 '12 at 05:24
  • 1
    What are you trying to do that would be different from a where condition? – Ilion Nov 30 '12 at 05:24
  • If the request without the condition, the names in which the records are repeated. For example WHERE name LIKE "City1" with the same name will return five rows but id they will be different. – Vayas Nov 30 '12 at 05:31
  • But if there is no match then display five records – Vayas Nov 30 '12 at 05:32
  • Example Search LIKE "City1%" display records three City1, City1, City1 – Vayas Nov 30 '12 at 05:35
  • Example 2. Search LIKE "City2%" display records three City2, City2, City2_en, City2_rect, City2_les – Vayas Nov 30 '12 at 05:36
  • um, i'm still not entirely clear on what you're looking for. is this it: if there is an exact match for 'name', only show one row (even though more than one row might match exactly), but if there is no exact match then show 5 rows? – caitriona Nov 30 '12 at 11:16

2 Answers2

0

no, it's not possible to have conditional limit statement like you have in your question.

if you're using a stored procedure it is possible to have parameters or local variables as your limit value as described here:

How to make limit offset dynamic using only (My)SQL

and if you don't want to use a stored procedure, here's some sql that will return one row if there's an exact name match or else 5 rows if there's a partial name match:

SELECT * FROM mlt_adr_city
    WHERE
    name = 'Text'
    AND region_id = 59
    AND id <> 0
    LIMIT 1
UNION
SELECT * FROM mlt_adr_city
    WHERE
    name like 'Text%'
    AND region_id = 59
    AND id <> 0
    AND NOT EXISTS (SELECT 1 FROM mlt_adr_city WHERE name = 'Text' AND region_id = 59 AND id <> 0)
    LIMIT 5;
Community
  • 1
  • 1
caitriona
  • 8,569
  • 4
  • 32
  • 36
0

Example

SELECT * FROM mlt_adr_city WHERE name LIKE "Text%"  AND region_id = 59 AND id <> 0 GROUP BY name LIMIT 5
Vayas
  • 107
  • 3
  • 11