MySql find matches between words using query in Java












0















I have Java program that is connected to a mysql database. What I want to do is take user input and display a column that matches with the user input. The column contains a list of programming languages. I need help with the sql query needed to do this. For example if the column in the first row contained the languages Javascript, C++, C# and the column in the second row contained Java and if I used the query



"SELECT * from Language WHERE Programming Languages LIKE'%"+input.getText()+"%'")


and the user were to search for Java then the table would display the column that has Java and Javascript because it has the word Java. And if there was a third row and that column had C , and if the user were to search C, then it would display the columns with C++ and C#.



I tried to use this regex instead



`"SELECT * from Language WHERE Programming Language REGEXP'[[<:]]"+input.getText()+"[[:>:]]'")`


which solved the Java and Javascript problem, but searching for C would display the columns with C# and C++. Also searching for C# would display nothing and searching for C++ would throw this error repetition operator operand invalid.
My Table



The table's column data:



Javascript HTML CSS PHP
C# Java
C
C++ C#


I tried to put a space before % in the first query which works with differentiating. My question now is how do i search for multiple words in a column, the above query works for two or three inputs in exact order. Searching javascript and html would show but javascript and css would not show because its not in order










share|improve this question

























  • Can you show some example data in the table columns/rows?

    – prasad_
    Nov 26 '18 at 2:55













  • just added the image, dont have enough rep to embed, so its posted as a link

    – Samurai Bale
    Nov 26 '18 at 3:00











  • You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

    – prasad_
    Nov 26 '18 at 3:21


















0















I have Java program that is connected to a mysql database. What I want to do is take user input and display a column that matches with the user input. The column contains a list of programming languages. I need help with the sql query needed to do this. For example if the column in the first row contained the languages Javascript, C++, C# and the column in the second row contained Java and if I used the query



"SELECT * from Language WHERE Programming Languages LIKE'%"+input.getText()+"%'")


and the user were to search for Java then the table would display the column that has Java and Javascript because it has the word Java. And if there was a third row and that column had C , and if the user were to search C, then it would display the columns with C++ and C#.



I tried to use this regex instead



`"SELECT * from Language WHERE Programming Language REGEXP'[[<:]]"+input.getText()+"[[:>:]]'")`


which solved the Java and Javascript problem, but searching for C would display the columns with C# and C++. Also searching for C# would display nothing and searching for C++ would throw this error repetition operator operand invalid.
My Table



The table's column data:



Javascript HTML CSS PHP
C# Java
C
C++ C#


I tried to put a space before % in the first query which works with differentiating. My question now is how do i search for multiple words in a column, the above query works for two or three inputs in exact order. Searching javascript and html would show but javascript and css would not show because its not in order










share|improve this question

























  • Can you show some example data in the table columns/rows?

    – prasad_
    Nov 26 '18 at 2:55













  • just added the image, dont have enough rep to embed, so its posted as a link

    – Samurai Bale
    Nov 26 '18 at 3:00











  • You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

    – prasad_
    Nov 26 '18 at 3:21
















0












0








0








I have Java program that is connected to a mysql database. What I want to do is take user input and display a column that matches with the user input. The column contains a list of programming languages. I need help with the sql query needed to do this. For example if the column in the first row contained the languages Javascript, C++, C# and the column in the second row contained Java and if I used the query



"SELECT * from Language WHERE Programming Languages LIKE'%"+input.getText()+"%'")


and the user were to search for Java then the table would display the column that has Java and Javascript because it has the word Java. And if there was a third row and that column had C , and if the user were to search C, then it would display the columns with C++ and C#.



I tried to use this regex instead



`"SELECT * from Language WHERE Programming Language REGEXP'[[<:]]"+input.getText()+"[[:>:]]'")`


which solved the Java and Javascript problem, but searching for C would display the columns with C# and C++. Also searching for C# would display nothing and searching for C++ would throw this error repetition operator operand invalid.
My Table



The table's column data:



Javascript HTML CSS PHP
C# Java
C
C++ C#


I tried to put a space before % in the first query which works with differentiating. My question now is how do i search for multiple words in a column, the above query works for two or three inputs in exact order. Searching javascript and html would show but javascript and css would not show because its not in order










share|improve this question
















I have Java program that is connected to a mysql database. What I want to do is take user input and display a column that matches with the user input. The column contains a list of programming languages. I need help with the sql query needed to do this. For example if the column in the first row contained the languages Javascript, C++, C# and the column in the second row contained Java and if I used the query



"SELECT * from Language WHERE Programming Languages LIKE'%"+input.getText()+"%'")


and the user were to search for Java then the table would display the column that has Java and Javascript because it has the word Java. And if there was a third row and that column had C , and if the user were to search C, then it would display the columns with C++ and C#.



I tried to use this regex instead



`"SELECT * from Language WHERE Programming Language REGEXP'[[<:]]"+input.getText()+"[[:>:]]'")`


which solved the Java and Javascript problem, but searching for C would display the columns with C# and C++. Also searching for C# would display nothing and searching for C++ would throw this error repetition operator operand invalid.
My Table



The table's column data:



Javascript HTML CSS PHP
C# Java
C
C++ C#


I tried to put a space before % in the first query which works with differentiating. My question now is how do i search for multiple words in a column, the above query works for two or three inputs in exact order. Searching javascript and html would show but javascript and css would not show because its not in order







java mysql jdbc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 8:14







Samurai Bale

















asked Nov 26 '18 at 2:50









Samurai BaleSamurai Bale

62




62













  • Can you show some example data in the table columns/rows?

    – prasad_
    Nov 26 '18 at 2:55













  • just added the image, dont have enough rep to embed, so its posted as a link

    – Samurai Bale
    Nov 26 '18 at 3:00











  • You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

    – prasad_
    Nov 26 '18 at 3:21





















  • Can you show some example data in the table columns/rows?

    – prasad_
    Nov 26 '18 at 2:55













  • just added the image, dont have enough rep to embed, so its posted as a link

    – Samurai Bale
    Nov 26 '18 at 3:00











  • You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

    – prasad_
    Nov 26 '18 at 3:21



















Can you show some example data in the table columns/rows?

– prasad_
Nov 26 '18 at 2:55







Can you show some example data in the table columns/rows?

– prasad_
Nov 26 '18 at 2:55















just added the image, dont have enough rep to embed, so its posted as a link

– Samurai Bale
Nov 26 '18 at 3:00





just added the image, dont have enough rep to embed, so its posted as a link

– Samurai Bale
Nov 26 '18 at 3:00













You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

– prasad_
Nov 26 '18 at 3:21







You may want to try something like splitting a column's value into multiple substrings and work with it. Try searching/google with "mysql split string column" for some ideas.

– prasad_
Nov 26 '18 at 3:21














3 Answers
3






active

oldest

votes


















0














Let me just guess, you store all developers to a table, and one of the columns stores the languages that this developer masters.



When someone input a language, you want to search all the developers who master this language?



If so, I would do this, in your language column, in your language column, add an extra white space to the end (use _ as white space below)



Javascript_HTML_CSS_PHP_
C#_Java_
C++_C#_
C_
Python_Java_LUA_


Then in your Java code, you could use the following code to do this.



SELECT * from Language WHERE Programming_Languages LIKE'%"+input.getText()+"_%'")


And please pay attention that you should never concatenate sql string in your Java code. You can do it in this way:



statment = con.prepare("SELECT * from Language WHERE Programming_Languages LIKE ?")
statment.setString(1, "%" + input.getText() + "_%")





share|improve this answer
























  • This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

    – Johannes H.
    Nov 26 '18 at 3:50













  • @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

    – Keijack
    Nov 26 '18 at 4:00











  • It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

    – Johannes H.
    Nov 26 '18 at 4:02











  • It would still show Javascript , if i to were search for Java

    – Samurai Bale
    Nov 26 '18 at 4:07








  • 1





    I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

    – Keijack
    Nov 26 '18 at 8:49





















0














Your issue here is that your database model is not correctly normalized. While possible, it is not recommended to store multiple values in one column of a database. Instead, as a first step, use a seperate database to store the proghramming languages, and use unique indexes to map the values to people:



people

id | name | other stuff
86511 | Bob | ...
86513 | TBA | ...


.



languages

language | user_id
C# | 86511
Java | 86511
C++ | 86513
C# | 86513


Now you can just search the table LANGUAGES for the corresponding language and map it o the users using a JOIN-clause:



SELECT *
FROM languages
INNER JOIN people ON languages.user_id = people.id
WHERE languages.language = 'C#';


However, there is still room for optimizations: you now have multiple entries for each programming language in the languages table. This takes longer to search and has a potential to result in inconsistent databases if you accidentally have a typo in only one of the entries for the same language, for example.
To avoid all this, we can introduce a third table that only holds one entry for each langauge, and restructure the second table to be only a mapping:



 people

id | name | other stuff
86511 | Bob | ...
86513 | TBA | ...


.



lang_mapping

lang_id | user_id
1 | 86511
2 | 86511
3 | 86513
1 | 86513


.



languages

id | language
1 | C#
2 | Java
3 | C++


You can still query the tables and connect languages to usery by introducing another join to the query:



SELECT *
FROM languages
INNER JOIN lang_mapping ON lang_mapping.lang_id = language.id
INNER JOIN people ON lang_mapping.user_id = people.id
WHERE languages.language = 'C#'


NOw you have only one entry per language, you can easily search for each language and get all mapped users, and you can add individual new languages or mappings withgout modifying the users table at all.



This is the normalized form of your data model.






share|improve this answer

































    0














    Here is a way to do the query. This is derived from this post.



    This requires a temporary table with number of rows populated by numbers up to the maximum number of tokens (language strings separated by space, e.g., the string "Javascript HTML CSS PHP" has 4 tokens).



    Create a temp table with numbers:



    create temporary table temp_numbers as
    select 1 as num
    union all select 2 as num
    union all select 3 as num
    union all select 4 as num
    union all select 5 as num
    ;



    Table with a column of programming language strings:



    create table test_langs (
    col1 varchar(10) NOT NULL,
    col2 varchar(50) NOT NULL
    );


    Insert some test data:



    insert into test_langs values ('ID-1', 'Javascript HTML CSS PHP');
    insert into test_langs values ('ID-2', 'C# Java');
    insert into test_langs values ('ID-3', 'C');
    insert into test_langs values ('ID-4', 'C# C++');
    insert into test_langs values ('ID-5', 'Python Java LUA');



    The query:



    This select gets the exact match to the input string "C";



    select col1, token from (
    select col1, substring_index(
    substring_index(col2, ' ', num),
    ' ',
    -1
    ) as token
    from test_langs
    join temp_numbers
    on char_length(col2) - char_length(replace(col2, ' ', '')) >= num - 1
    ) as individual_progamming_languages
    where token='C';


    The result:



    +------+-------+
    | col1 | token |
    +------+-------+
    | ID-3 | C |
    +------+-------+





    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f53474183%2fmysql-find-matches-between-words-using-query-in-java%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Let me just guess, you store all developers to a table, and one of the columns stores the languages that this developer masters.



      When someone input a language, you want to search all the developers who master this language?



      If so, I would do this, in your language column, in your language column, add an extra white space to the end (use _ as white space below)



      Javascript_HTML_CSS_PHP_
      C#_Java_
      C++_C#_
      C_
      Python_Java_LUA_


      Then in your Java code, you could use the following code to do this.



      SELECT * from Language WHERE Programming_Languages LIKE'%"+input.getText()+"_%'")


      And please pay attention that you should never concatenate sql string in your Java code. You can do it in this way:



      statment = con.prepare("SELECT * from Language WHERE Programming_Languages LIKE ?")
      statment.setString(1, "%" + input.getText() + "_%")





      share|improve this answer
























      • This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

        – Johannes H.
        Nov 26 '18 at 3:50













      • @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

        – Keijack
        Nov 26 '18 at 4:00











      • It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

        – Johannes H.
        Nov 26 '18 at 4:02











      • It would still show Javascript , if i to were search for Java

        – Samurai Bale
        Nov 26 '18 at 4:07








      • 1





        I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

        – Keijack
        Nov 26 '18 at 8:49


















      0














      Let me just guess, you store all developers to a table, and one of the columns stores the languages that this developer masters.



      When someone input a language, you want to search all the developers who master this language?



      If so, I would do this, in your language column, in your language column, add an extra white space to the end (use _ as white space below)



      Javascript_HTML_CSS_PHP_
      C#_Java_
      C++_C#_
      C_
      Python_Java_LUA_


      Then in your Java code, you could use the following code to do this.



      SELECT * from Language WHERE Programming_Languages LIKE'%"+input.getText()+"_%'")


      And please pay attention that you should never concatenate sql string in your Java code. You can do it in this way:



      statment = con.prepare("SELECT * from Language WHERE Programming_Languages LIKE ?")
      statment.setString(1, "%" + input.getText() + "_%")





      share|improve this answer
























      • This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

        – Johannes H.
        Nov 26 '18 at 3:50













      • @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

        – Keijack
        Nov 26 '18 at 4:00











      • It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

        – Johannes H.
        Nov 26 '18 at 4:02











      • It would still show Javascript , if i to were search for Java

        – Samurai Bale
        Nov 26 '18 at 4:07








      • 1





        I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

        – Keijack
        Nov 26 '18 at 8:49
















      0












      0








      0







      Let me just guess, you store all developers to a table, and one of the columns stores the languages that this developer masters.



      When someone input a language, you want to search all the developers who master this language?



      If so, I would do this, in your language column, in your language column, add an extra white space to the end (use _ as white space below)



      Javascript_HTML_CSS_PHP_
      C#_Java_
      C++_C#_
      C_
      Python_Java_LUA_


      Then in your Java code, you could use the following code to do this.



      SELECT * from Language WHERE Programming_Languages LIKE'%"+input.getText()+"_%'")


      And please pay attention that you should never concatenate sql string in your Java code. You can do it in this way:



      statment = con.prepare("SELECT * from Language WHERE Programming_Languages LIKE ?")
      statment.setString(1, "%" + input.getText() + "_%")





      share|improve this answer













      Let me just guess, you store all developers to a table, and one of the columns stores the languages that this developer masters.



      When someone input a language, you want to search all the developers who master this language?



      If so, I would do this, in your language column, in your language column, add an extra white space to the end (use _ as white space below)



      Javascript_HTML_CSS_PHP_
      C#_Java_
      C++_C#_
      C_
      Python_Java_LUA_


      Then in your Java code, you could use the following code to do this.



      SELECT * from Language WHERE Programming_Languages LIKE'%"+input.getText()+"_%'")


      And please pay attention that you should never concatenate sql string in your Java code. You can do it in this way:



      statment = con.prepare("SELECT * from Language WHERE Programming_Languages LIKE ?")
      statment.setString(1, "%" + input.getText() + "_%")






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 26 '18 at 3:43









      KeijackKeijack

      20117




      20117













      • This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

        – Johannes H.
        Nov 26 '18 at 3:50













      • @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

        – Keijack
        Nov 26 '18 at 4:00











      • It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

        – Johannes H.
        Nov 26 '18 at 4:02











      • It would still show Javascript , if i to were search for Java

        – Samurai Bale
        Nov 26 '18 at 4:07








      • 1





        I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

        – Keijack
        Nov 26 '18 at 8:49





















      • This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

        – Johannes H.
        Nov 26 '18 at 3:50













      • @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

        – Keijack
        Nov 26 '18 at 4:00











      • It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

        – Johannes H.
        Nov 26 '18 at 4:02











      • It would still show Javascript , if i to were search for Java

        – Samurai Bale
        Nov 26 '18 at 4:07








      • 1





        I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

        – Keijack
        Nov 26 '18 at 8:49



















      This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

      – Johannes H.
      Nov 26 '18 at 3:50







      This solution is not really any better than the original one - instead of using spaces as separator, you now use an underscore. It is not much different from looking for LIKE '%LANG %' in his original query (which would work exactly the same as your approach if he inserts additional spaces at the end of every string). However your approach also introduces an additional issue in case any programming language that has an udnerscore in its name is invented any time ;)

      – Johannes H.
      Nov 26 '18 at 3:50















      @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

      – Keijack
      Nov 26 '18 at 4:00





      @JohannesH. Yes, I think adding a white space to every string is the easiest way. I just use _ to represent the white space.

      – Keijack
      Nov 26 '18 at 4:00













      It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

      – Johannes H.
      Nov 26 '18 at 4:02





      It might be the quickest approach. Doesn't make it the easiest. It's easy to, for example, forget to add a space when editing data, or in a string inside the applicaiton logic. ALso not that full text search in databases is a very slow operation compared to others, so you want to avoid it.

      – Johannes H.
      Nov 26 '18 at 4:02













      It would still show Javascript , if i to were search for Java

      – Samurai Bale
      Nov 26 '18 at 4:07







      It would still show Javascript , if i to were search for Java

      – Samurai Bale
      Nov 26 '18 at 4:07






      1




      1





      I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

      – Keijack
      Nov 26 '18 at 8:49







      I agree with @JohannesH. if you still want to use this solution, you should use and statement. You will first break the user input into 2 works and use ·SELECT * from Language WHERE `Programming Languages` LIKE '%Javascript %' and `Programming Languages` LIKE '%CSS %'

      – Keijack
      Nov 26 '18 at 8:49















      0














      Your issue here is that your database model is not correctly normalized. While possible, it is not recommended to store multiple values in one column of a database. Instead, as a first step, use a seperate database to store the proghramming languages, and use unique indexes to map the values to people:



      people

      id | name | other stuff
      86511 | Bob | ...
      86513 | TBA | ...


      .



      languages

      language | user_id
      C# | 86511
      Java | 86511
      C++ | 86513
      C# | 86513


      Now you can just search the table LANGUAGES for the corresponding language and map it o the users using a JOIN-clause:



      SELECT *
      FROM languages
      INNER JOIN people ON languages.user_id = people.id
      WHERE languages.language = 'C#';


      However, there is still room for optimizations: you now have multiple entries for each programming language in the languages table. This takes longer to search and has a potential to result in inconsistent databases if you accidentally have a typo in only one of the entries for the same language, for example.
      To avoid all this, we can introduce a third table that only holds one entry for each langauge, and restructure the second table to be only a mapping:



       people

      id | name | other stuff
      86511 | Bob | ...
      86513 | TBA | ...


      .



      lang_mapping

      lang_id | user_id
      1 | 86511
      2 | 86511
      3 | 86513
      1 | 86513


      .



      languages

      id | language
      1 | C#
      2 | Java
      3 | C++


      You can still query the tables and connect languages to usery by introducing another join to the query:



      SELECT *
      FROM languages
      INNER JOIN lang_mapping ON lang_mapping.lang_id = language.id
      INNER JOIN people ON lang_mapping.user_id = people.id
      WHERE languages.language = 'C#'


      NOw you have only one entry per language, you can easily search for each language and get all mapped users, and you can add individual new languages or mappings withgout modifying the users table at all.



      This is the normalized form of your data model.






      share|improve this answer






























        0














        Your issue here is that your database model is not correctly normalized. While possible, it is not recommended to store multiple values in one column of a database. Instead, as a first step, use a seperate database to store the proghramming languages, and use unique indexes to map the values to people:



        people

        id | name | other stuff
        86511 | Bob | ...
        86513 | TBA | ...


        .



        languages

        language | user_id
        C# | 86511
        Java | 86511
        C++ | 86513
        C# | 86513


        Now you can just search the table LANGUAGES for the corresponding language and map it o the users using a JOIN-clause:



        SELECT *
        FROM languages
        INNER JOIN people ON languages.user_id = people.id
        WHERE languages.language = 'C#';


        However, there is still room for optimizations: you now have multiple entries for each programming language in the languages table. This takes longer to search and has a potential to result in inconsistent databases if you accidentally have a typo in only one of the entries for the same language, for example.
        To avoid all this, we can introduce a third table that only holds one entry for each langauge, and restructure the second table to be only a mapping:



         people

        id | name | other stuff
        86511 | Bob | ...
        86513 | TBA | ...


        .



        lang_mapping

        lang_id | user_id
        1 | 86511
        2 | 86511
        3 | 86513
        1 | 86513


        .



        languages

        id | language
        1 | C#
        2 | Java
        3 | C++


        You can still query the tables and connect languages to usery by introducing another join to the query:



        SELECT *
        FROM languages
        INNER JOIN lang_mapping ON lang_mapping.lang_id = language.id
        INNER JOIN people ON lang_mapping.user_id = people.id
        WHERE languages.language = 'C#'


        NOw you have only one entry per language, you can easily search for each language and get all mapped users, and you can add individual new languages or mappings withgout modifying the users table at all.



        This is the normalized form of your data model.






        share|improve this answer




























          0












          0








          0







          Your issue here is that your database model is not correctly normalized. While possible, it is not recommended to store multiple values in one column of a database. Instead, as a first step, use a seperate database to store the proghramming languages, and use unique indexes to map the values to people:



          people

          id | name | other stuff
          86511 | Bob | ...
          86513 | TBA | ...


          .



          languages

          language | user_id
          C# | 86511
          Java | 86511
          C++ | 86513
          C# | 86513


          Now you can just search the table LANGUAGES for the corresponding language and map it o the users using a JOIN-clause:



          SELECT *
          FROM languages
          INNER JOIN people ON languages.user_id = people.id
          WHERE languages.language = 'C#';


          However, there is still room for optimizations: you now have multiple entries for each programming language in the languages table. This takes longer to search and has a potential to result in inconsistent databases if you accidentally have a typo in only one of the entries for the same language, for example.
          To avoid all this, we can introduce a third table that only holds one entry for each langauge, and restructure the second table to be only a mapping:



           people

          id | name | other stuff
          86511 | Bob | ...
          86513 | TBA | ...


          .



          lang_mapping

          lang_id | user_id
          1 | 86511
          2 | 86511
          3 | 86513
          1 | 86513


          .



          languages

          id | language
          1 | C#
          2 | Java
          3 | C++


          You can still query the tables and connect languages to usery by introducing another join to the query:



          SELECT *
          FROM languages
          INNER JOIN lang_mapping ON lang_mapping.lang_id = language.id
          INNER JOIN people ON lang_mapping.user_id = people.id
          WHERE languages.language = 'C#'


          NOw you have only one entry per language, you can easily search for each language and get all mapped users, and you can add individual new languages or mappings withgout modifying the users table at all.



          This is the normalized form of your data model.






          share|improve this answer















          Your issue here is that your database model is not correctly normalized. While possible, it is not recommended to store multiple values in one column of a database. Instead, as a first step, use a seperate database to store the proghramming languages, and use unique indexes to map the values to people:



          people

          id | name | other stuff
          86511 | Bob | ...
          86513 | TBA | ...


          .



          languages

          language | user_id
          C# | 86511
          Java | 86511
          C++ | 86513
          C# | 86513


          Now you can just search the table LANGUAGES for the corresponding language and map it o the users using a JOIN-clause:



          SELECT *
          FROM languages
          INNER JOIN people ON languages.user_id = people.id
          WHERE languages.language = 'C#';


          However, there is still room for optimizations: you now have multiple entries for each programming language in the languages table. This takes longer to search and has a potential to result in inconsistent databases if you accidentally have a typo in only one of the entries for the same language, for example.
          To avoid all this, we can introduce a third table that only holds one entry for each langauge, and restructure the second table to be only a mapping:



           people

          id | name | other stuff
          86511 | Bob | ...
          86513 | TBA | ...


          .



          lang_mapping

          lang_id | user_id
          1 | 86511
          2 | 86511
          3 | 86513
          1 | 86513


          .



          languages

          id | language
          1 | C#
          2 | Java
          3 | C++


          You can still query the tables and connect languages to usery by introducing another join to the query:



          SELECT *
          FROM languages
          INNER JOIN lang_mapping ON lang_mapping.lang_id = language.id
          INNER JOIN people ON lang_mapping.user_id = people.id
          WHERE languages.language = 'C#'


          NOw you have only one entry per language, you can easily search for each language and get all mapped users, and you can add individual new languages or mappings withgout modifying the users table at all.



          This is the normalized form of your data model.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 3:58

























          answered Nov 26 '18 at 3:48









          Johannes H.Johannes H.

          4,6241435




          4,6241435























              0














              Here is a way to do the query. This is derived from this post.



              This requires a temporary table with number of rows populated by numbers up to the maximum number of tokens (language strings separated by space, e.g., the string "Javascript HTML CSS PHP" has 4 tokens).



              Create a temp table with numbers:



              create temporary table temp_numbers as
              select 1 as num
              union all select 2 as num
              union all select 3 as num
              union all select 4 as num
              union all select 5 as num
              ;



              Table with a column of programming language strings:



              create table test_langs (
              col1 varchar(10) NOT NULL,
              col2 varchar(50) NOT NULL
              );


              Insert some test data:



              insert into test_langs values ('ID-1', 'Javascript HTML CSS PHP');
              insert into test_langs values ('ID-2', 'C# Java');
              insert into test_langs values ('ID-3', 'C');
              insert into test_langs values ('ID-4', 'C# C++');
              insert into test_langs values ('ID-5', 'Python Java LUA');



              The query:



              This select gets the exact match to the input string "C";



              select col1, token from (
              select col1, substring_index(
              substring_index(col2, ' ', num),
              ' ',
              -1
              ) as token
              from test_langs
              join temp_numbers
              on char_length(col2) - char_length(replace(col2, ' ', '')) >= num - 1
              ) as individual_progamming_languages
              where token='C';


              The result:



              +------+-------+
              | col1 | token |
              +------+-------+
              | ID-3 | C |
              +------+-------+





              share|improve this answer




























                0














                Here is a way to do the query. This is derived from this post.



                This requires a temporary table with number of rows populated by numbers up to the maximum number of tokens (language strings separated by space, e.g., the string "Javascript HTML CSS PHP" has 4 tokens).



                Create a temp table with numbers:



                create temporary table temp_numbers as
                select 1 as num
                union all select 2 as num
                union all select 3 as num
                union all select 4 as num
                union all select 5 as num
                ;



                Table with a column of programming language strings:



                create table test_langs (
                col1 varchar(10) NOT NULL,
                col2 varchar(50) NOT NULL
                );


                Insert some test data:



                insert into test_langs values ('ID-1', 'Javascript HTML CSS PHP');
                insert into test_langs values ('ID-2', 'C# Java');
                insert into test_langs values ('ID-3', 'C');
                insert into test_langs values ('ID-4', 'C# C++');
                insert into test_langs values ('ID-5', 'Python Java LUA');



                The query:



                This select gets the exact match to the input string "C";



                select col1, token from (
                select col1, substring_index(
                substring_index(col2, ' ', num),
                ' ',
                -1
                ) as token
                from test_langs
                join temp_numbers
                on char_length(col2) - char_length(replace(col2, ' ', '')) >= num - 1
                ) as individual_progamming_languages
                where token='C';


                The result:



                +------+-------+
                | col1 | token |
                +------+-------+
                | ID-3 | C |
                +------+-------+





                share|improve this answer


























                  0












                  0








                  0







                  Here is a way to do the query. This is derived from this post.



                  This requires a temporary table with number of rows populated by numbers up to the maximum number of tokens (language strings separated by space, e.g., the string "Javascript HTML CSS PHP" has 4 tokens).



                  Create a temp table with numbers:



                  create temporary table temp_numbers as
                  select 1 as num
                  union all select 2 as num
                  union all select 3 as num
                  union all select 4 as num
                  union all select 5 as num
                  ;



                  Table with a column of programming language strings:



                  create table test_langs (
                  col1 varchar(10) NOT NULL,
                  col2 varchar(50) NOT NULL
                  );


                  Insert some test data:



                  insert into test_langs values ('ID-1', 'Javascript HTML CSS PHP');
                  insert into test_langs values ('ID-2', 'C# Java');
                  insert into test_langs values ('ID-3', 'C');
                  insert into test_langs values ('ID-4', 'C# C++');
                  insert into test_langs values ('ID-5', 'Python Java LUA');



                  The query:



                  This select gets the exact match to the input string "C";



                  select col1, token from (
                  select col1, substring_index(
                  substring_index(col2, ' ', num),
                  ' ',
                  -1
                  ) as token
                  from test_langs
                  join temp_numbers
                  on char_length(col2) - char_length(replace(col2, ' ', '')) >= num - 1
                  ) as individual_progamming_languages
                  where token='C';


                  The result:



                  +------+-------+
                  | col1 | token |
                  +------+-------+
                  | ID-3 | C |
                  +------+-------+





                  share|improve this answer













                  Here is a way to do the query. This is derived from this post.



                  This requires a temporary table with number of rows populated by numbers up to the maximum number of tokens (language strings separated by space, e.g., the string "Javascript HTML CSS PHP" has 4 tokens).



                  Create a temp table with numbers:



                  create temporary table temp_numbers as
                  select 1 as num
                  union all select 2 as num
                  union all select 3 as num
                  union all select 4 as num
                  union all select 5 as num
                  ;



                  Table with a column of programming language strings:



                  create table test_langs (
                  col1 varchar(10) NOT NULL,
                  col2 varchar(50) NOT NULL
                  );


                  Insert some test data:



                  insert into test_langs values ('ID-1', 'Javascript HTML CSS PHP');
                  insert into test_langs values ('ID-2', 'C# Java');
                  insert into test_langs values ('ID-3', 'C');
                  insert into test_langs values ('ID-4', 'C# C++');
                  insert into test_langs values ('ID-5', 'Python Java LUA');



                  The query:



                  This select gets the exact match to the input string "C";



                  select col1, token from (
                  select col1, substring_index(
                  substring_index(col2, ' ', num),
                  ' ',
                  -1
                  ) as token
                  from test_langs
                  join temp_numbers
                  on char_length(col2) - char_length(replace(col2, ' ', '')) >= num - 1
                  ) as individual_progamming_languages
                  where token='C';


                  The result:



                  +------+-------+
                  | col1 | token |
                  +------+-------+
                  | ID-3 | C |
                  +------+-------+






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 7:22









                  prasad_prasad_

                  1,5781618




                  1,5781618






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474183%2fmysql-find-matches-between-words-using-query-in-java%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'