简体   繁体   English

搜索条件mysql IF(name = name,LIMIT 1,LIMIT 5)

[英]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. 例如WHERE名称LIKE“City1”具有相同的名称将返回五行但id将是不同的。

But if there is no match then display five records. 但如果没有匹配则显示五条记录。

Example Search LIKE "City1%" display records three City1, City1, City1. 示例搜索LIKE "City1%"显示记录三个City1,City1,City1。

Example 2. Search LIKE "City2%" display records three City2, City2, City2_en, City2_rect, City2_les. 示例2.搜索LIKE "City2%"显示记录三个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 如何仅使用(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: 如果你不想使用存储过程,这里有一些sql如果有一个确切的名字匹配将返回一行,如果有一个部分名称匹配则返回5行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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