If the word is in two columns then priority in the results in mysql











up vote
-1
down vote

favorite












I have a table like these:



menu_id menu_name menu_description
menus table



i would like to order the results giving a priority if the word is in the menu_name and menu description. How can i achieve this?



UPDATE:
with this query i get the most relevant results on top:



    select menu_id, menu_name, menu_description from ( select menu_id, 
menu_name, menu_description, case when menu_name like '%salame%'
then 1 else 0 end + case when menu_description like '%salame%'
then 1 else 0 end as order_value from z8upvan6w_menus) as t order
by order_value desc


results:



    menu_id | menu_name              | menu_description
13 | Panino Salame e Noci | Salame, Noci, Caprino e Insalata
14 | Piadina Salame e Noci | Salame, Noci, Caprino e Insalata
11 | Panino Caprese | Mozzarella, Pomodoro e Insalata
12 | Panino Boscaiolo | Speck, Brie e Salsa Boscaiola


my question is why the third and fourth record appear if there is no word "salame" in any field, how do i get rid of them?










share|improve this question




















  • 1




    What word? What is the menu_name? What is the "menu description"?
    – Gordon Linoff
    Nov 22 at 1:28










  • I updated the question, please have a look.
    – Robert Falco
    Nov 22 at 2:42










  • Because the records are only ordered, not filtered. I'll update this in my answer below.
    – Yatin
    Nov 22 at 22:50















up vote
-1
down vote

favorite












I have a table like these:



menu_id menu_name menu_description
menus table



i would like to order the results giving a priority if the word is in the menu_name and menu description. How can i achieve this?



UPDATE:
with this query i get the most relevant results on top:



    select menu_id, menu_name, menu_description from ( select menu_id, 
menu_name, menu_description, case when menu_name like '%salame%'
then 1 else 0 end + case when menu_description like '%salame%'
then 1 else 0 end as order_value from z8upvan6w_menus) as t order
by order_value desc


results:



    menu_id | menu_name              | menu_description
13 | Panino Salame e Noci | Salame, Noci, Caprino e Insalata
14 | Piadina Salame e Noci | Salame, Noci, Caprino e Insalata
11 | Panino Caprese | Mozzarella, Pomodoro e Insalata
12 | Panino Boscaiolo | Speck, Brie e Salsa Boscaiola


my question is why the third and fourth record appear if there is no word "salame" in any field, how do i get rid of them?










share|improve this question




















  • 1




    What word? What is the menu_name? What is the "menu description"?
    – Gordon Linoff
    Nov 22 at 1:28










  • I updated the question, please have a look.
    – Robert Falco
    Nov 22 at 2:42










  • Because the records are only ordered, not filtered. I'll update this in my answer below.
    – Yatin
    Nov 22 at 22:50













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a table like these:



menu_id menu_name menu_description
menus table



i would like to order the results giving a priority if the word is in the menu_name and menu description. How can i achieve this?



UPDATE:
with this query i get the most relevant results on top:



    select menu_id, menu_name, menu_description from ( select menu_id, 
menu_name, menu_description, case when menu_name like '%salame%'
then 1 else 0 end + case when menu_description like '%salame%'
then 1 else 0 end as order_value from z8upvan6w_menus) as t order
by order_value desc


results:



    menu_id | menu_name              | menu_description
13 | Panino Salame e Noci | Salame, Noci, Caprino e Insalata
14 | Piadina Salame e Noci | Salame, Noci, Caprino e Insalata
11 | Panino Caprese | Mozzarella, Pomodoro e Insalata
12 | Panino Boscaiolo | Speck, Brie e Salsa Boscaiola


my question is why the third and fourth record appear if there is no word "salame" in any field, how do i get rid of them?










share|improve this question















I have a table like these:



menu_id menu_name menu_description
menus table



i would like to order the results giving a priority if the word is in the menu_name and menu description. How can i achieve this?



UPDATE:
with this query i get the most relevant results on top:



    select menu_id, menu_name, menu_description from ( select menu_id, 
menu_name, menu_description, case when menu_name like '%salame%'
then 1 else 0 end + case when menu_description like '%salame%'
then 1 else 0 end as order_value from z8upvan6w_menus) as t order
by order_value desc


results:



    menu_id | menu_name              | menu_description
13 | Panino Salame e Noci | Salame, Noci, Caprino e Insalata
14 | Piadina Salame e Noci | Salame, Noci, Caprino e Insalata
11 | Panino Caprese | Mozzarella, Pomodoro e Insalata
12 | Panino Boscaiolo | Speck, Brie e Salsa Boscaiola


my question is why the third and fourth record appear if there is no word "salame" in any field, how do i get rid of them?







jquery mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 2:43

























asked Nov 22 at 1:12









Robert Falco

12




12








  • 1




    What word? What is the menu_name? What is the "menu description"?
    – Gordon Linoff
    Nov 22 at 1:28










  • I updated the question, please have a look.
    – Robert Falco
    Nov 22 at 2:42










  • Because the records are only ordered, not filtered. I'll update this in my answer below.
    – Yatin
    Nov 22 at 22:50














  • 1




    What word? What is the menu_name? What is the "menu description"?
    – Gordon Linoff
    Nov 22 at 1:28










  • I updated the question, please have a look.
    – Robert Falco
    Nov 22 at 2:42










  • Because the records are only ordered, not filtered. I'll update this in my answer below.
    – Yatin
    Nov 22 at 22:50








1




1




What word? What is the menu_name? What is the "menu description"?
– Gordon Linoff
Nov 22 at 1:28




What word? What is the menu_name? What is the "menu description"?
– Gordon Linoff
Nov 22 at 1:28












I updated the question, please have a look.
– Robert Falco
Nov 22 at 2:42




I updated the question, please have a look.
– Robert Falco
Nov 22 at 2:42












Because the records are only ordered, not filtered. I'll update this in my answer below.
– Yatin
Nov 22 at 22:50




Because the records are only ordered, not filtered. I'll update this in my answer below.
– Yatin
Nov 22 at 22:50












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You are not filtering out any records. For that, you need a where clause:



select menu_id, menu_name, menu_description
from from z8upvan6w_menus
where (menu_name like '%salame%') or
(menu_description like '%salame%' )
order by ( (menu_name like '%salame%') +
(menu_description like '%salame%')
) desc;


Notes:




  • The where clause does the filtering.

  • No subquery is necessary. You can order by an expression.

  • I revised the expression so it uses the fact the MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.






share|improve this answer




























    up vote
    0
    down vote













    I'd think you can use a dynamic numeric order with value 1 when the name is in the menu_name and 1 when the name is in menu_description. Sum them up when you select and order the results by the summed up value.
    Like this:



    It's written according to the SQL Server syntax, but I think you should be able to convert it the way you want.



    UPDATE: Adding where clause to the query below to filter out the records which don't match both the menu_name and menu_description.



    Assuming @input_val is the value you're trying to find in the columns.



    select menu_id, menu_name, menu_description
    from (
    select menu_id, menu_name, menu_description,
    case
    when menu_name like '%' + @input_val + '%' then 1
    else 0
    end
    +
    case
    when menu_description like '%' + @input_val + '%' then 1
    else 0
    end
    as order_value
    from menu_table) as t
    where t.order_value > 0
    order by order_value desc, menu_name asc





    share|improve this answer























    • thank you @Yatin, please read the update.
      – Robert Falco
      Nov 22 at 2:41











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422605%2fif-the-word-is-in-two-columns-then-priority-in-the-results-in-mysql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You are not filtering out any records. For that, you need a where clause:



    select menu_id, menu_name, menu_description
    from from z8upvan6w_menus
    where (menu_name like '%salame%') or
    (menu_description like '%salame%' )
    order by ( (menu_name like '%salame%') +
    (menu_description like '%salame%')
    ) desc;


    Notes:




    • The where clause does the filtering.

    • No subquery is necessary. You can order by an expression.

    • I revised the expression so it uses the fact the MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.






    share|improve this answer

























      up vote
      0
      down vote













      You are not filtering out any records. For that, you need a where clause:



      select menu_id, menu_name, menu_description
      from from z8upvan6w_menus
      where (menu_name like '%salame%') or
      (menu_description like '%salame%' )
      order by ( (menu_name like '%salame%') +
      (menu_description like '%salame%')
      ) desc;


      Notes:




      • The where clause does the filtering.

      • No subquery is necessary. You can order by an expression.

      • I revised the expression so it uses the fact the MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You are not filtering out any records. For that, you need a where clause:



        select menu_id, menu_name, menu_description
        from from z8upvan6w_menus
        where (menu_name like '%salame%') or
        (menu_description like '%salame%' )
        order by ( (menu_name like '%salame%') +
        (menu_description like '%salame%')
        ) desc;


        Notes:




        • The where clause does the filtering.

        • No subquery is necessary. You can order by an expression.

        • I revised the expression so it uses the fact the MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.






        share|improve this answer












        You are not filtering out any records. For that, you need a where clause:



        select menu_id, menu_name, menu_description
        from from z8upvan6w_menus
        where (menu_name like '%salame%') or
        (menu_description like '%salame%' )
        order by ( (menu_name like '%salame%') +
        (menu_description like '%salame%')
        ) desc;


        Notes:




        • The where clause does the filtering.

        • No subquery is necessary. You can order by an expression.

        • I revised the expression so it uses the fact the MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 4:11









        Gordon Linoff

        753k35286395




        753k35286395
























            up vote
            0
            down vote













            I'd think you can use a dynamic numeric order with value 1 when the name is in the menu_name and 1 when the name is in menu_description. Sum them up when you select and order the results by the summed up value.
            Like this:



            It's written according to the SQL Server syntax, but I think you should be able to convert it the way you want.



            UPDATE: Adding where clause to the query below to filter out the records which don't match both the menu_name and menu_description.



            Assuming @input_val is the value you're trying to find in the columns.



            select menu_id, menu_name, menu_description
            from (
            select menu_id, menu_name, menu_description,
            case
            when menu_name like '%' + @input_val + '%' then 1
            else 0
            end
            +
            case
            when menu_description like '%' + @input_val + '%' then 1
            else 0
            end
            as order_value
            from menu_table) as t
            where t.order_value > 0
            order by order_value desc, menu_name asc





            share|improve this answer























            • thank you @Yatin, please read the update.
              – Robert Falco
              Nov 22 at 2:41















            up vote
            0
            down vote













            I'd think you can use a dynamic numeric order with value 1 when the name is in the menu_name and 1 when the name is in menu_description. Sum them up when you select and order the results by the summed up value.
            Like this:



            It's written according to the SQL Server syntax, but I think you should be able to convert it the way you want.



            UPDATE: Adding where clause to the query below to filter out the records which don't match both the menu_name and menu_description.



            Assuming @input_val is the value you're trying to find in the columns.



            select menu_id, menu_name, menu_description
            from (
            select menu_id, menu_name, menu_description,
            case
            when menu_name like '%' + @input_val + '%' then 1
            else 0
            end
            +
            case
            when menu_description like '%' + @input_val + '%' then 1
            else 0
            end
            as order_value
            from menu_table) as t
            where t.order_value > 0
            order by order_value desc, menu_name asc





            share|improve this answer























            • thank you @Yatin, please read the update.
              – Robert Falco
              Nov 22 at 2:41













            up vote
            0
            down vote










            up vote
            0
            down vote









            I'd think you can use a dynamic numeric order with value 1 when the name is in the menu_name and 1 when the name is in menu_description. Sum them up when you select and order the results by the summed up value.
            Like this:



            It's written according to the SQL Server syntax, but I think you should be able to convert it the way you want.



            UPDATE: Adding where clause to the query below to filter out the records which don't match both the menu_name and menu_description.



            Assuming @input_val is the value you're trying to find in the columns.



            select menu_id, menu_name, menu_description
            from (
            select menu_id, menu_name, menu_description,
            case
            when menu_name like '%' + @input_val + '%' then 1
            else 0
            end
            +
            case
            when menu_description like '%' + @input_val + '%' then 1
            else 0
            end
            as order_value
            from menu_table) as t
            where t.order_value > 0
            order by order_value desc, menu_name asc





            share|improve this answer














            I'd think you can use a dynamic numeric order with value 1 when the name is in the menu_name and 1 when the name is in menu_description. Sum them up when you select and order the results by the summed up value.
            Like this:



            It's written according to the SQL Server syntax, but I think you should be able to convert it the way you want.



            UPDATE: Adding where clause to the query below to filter out the records which don't match both the menu_name and menu_description.



            Assuming @input_val is the value you're trying to find in the columns.



            select menu_id, menu_name, menu_description
            from (
            select menu_id, menu_name, menu_description,
            case
            when menu_name like '%' + @input_val + '%' then 1
            else 0
            end
            +
            case
            when menu_description like '%' + @input_val + '%' then 1
            else 0
            end
            as order_value
            from menu_table) as t
            where t.order_value > 0
            order by order_value desc, menu_name asc






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 at 22:53

























            answered Nov 22 at 1:53









            Yatin

            968712




            968712












            • thank you @Yatin, please read the update.
              – Robert Falco
              Nov 22 at 2:41


















            • thank you @Yatin, please read the update.
              – Robert Falco
              Nov 22 at 2:41
















            thank you @Yatin, please read the update.
            – Robert Falco
            Nov 22 at 2:41




            thank you @Yatin, please read the update.
            – Robert Falco
            Nov 22 at 2:41


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422605%2fif-the-word-is-in-two-columns-then-priority-in-the-results-in-mysql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Sphinx de Gizeh