简体   繁体   中英

Search condition mysql IF (name = name, LIMIT 1, LIMIT 5)

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.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM