MySql Query, Select greater than

后端 未结 4 2298
我在风中等你
我在风中等你 2021-02-20 03:26

I\'ve got a table, called faq_questions with the following structure:

id int not_null auto_increment,
question varchar(255),
sort_order int

I\'

相关标签:
4条回答
  • 2021-02-20 04:08

    It seems too simple, but it looks like what you need:

    SELECT id,question FROM `questions` 
    WHERE `sort_order` > sort_order_variable
    ORDER BY sort_order ASC 
    LIMIT 1
    
    0 讨论(0)
  • 2021-02-20 04:08

    You can do it with TOP or LIMIT:

    SELECT TOP 1 * FROM faq_questions
    WHERE sort_order > 5
    ORDER BY sort_order ASC
    

    but that's not as elegant or portable as

    SELECT *  
    FROM faq_questions AS f1  
    LEFT JOIN faq_questions AS f2  
        ON f1.sort_order > f2.sort_order  
        AND f2.sort_order = 5  
    LEFT JOIN faq_questions AS f3  
        ON f3.sort_order BETWEEN f1.sort_order AND f2.sort_order  
    WHERE f3.id IS NULL
    
    0 讨论(0)
  • 2021-02-20 04:14
    SELECT * FROM table_name WHERE sort_order > 5 ORDER BY sort_order ASC LIMIT 1
    
    0 讨论(0)
  • 2021-02-20 04:33
    SELECT 
        id, question, sort_order
    FROM faq_questions 
    WHERE sort_order in 
    (SELECT 
            MIN(sort_order) 
        FROM faq_questions 
        WHERE sort_order > ?);
    

    That seems to work

    0 讨论(0)
提交回复
热议问题