Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. matching result from two different columns

matching result from two different columns

Scheduled Pinned Locked Moved MySQL
databasemysqlregextutorial
6 Posts 3 Posters 64 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RC123GUA
    wrote on last edited by
    #1

    I have a mysql table (tblNames)with columns: colA, colB, MiddleName, WholeName. with data like: colA = aaa colB = bbb MiddleName = Alan WholeName = JohnAlanSmith I would like to get a result of all the data where the MiddleName equals the middle part of the WholeName column starting at 4 and a length of 4 characters, for example where the MiddleName column contains "Alan" and the Mid Function result of the WholeName column equals "Alan", where they match. I am using following Mid Function to get the middle part of the WholeName column:

    SELECT Mid([WholeName ],4,7) AS "MatchingResult"
    FROM tblNames;

    The result could be a query or a new table.

    Richard Andrew x64R 1 Reply Last reply
    0
    • R RC123GUA

      I have a mysql table (tblNames)with columns: colA, colB, MiddleName, WholeName. with data like: colA = aaa colB = bbb MiddleName = Alan WholeName = JohnAlanSmith I would like to get a result of all the data where the MiddleName equals the middle part of the WholeName column starting at 4 and a length of 4 characters, for example where the MiddleName column contains "Alan" and the Mid Function result of the WholeName column equals "Alan", where they match. I am using following Mid Function to get the middle part of the WholeName column:

      SELECT Mid([WholeName ],4,7) AS "MatchingResult"
      FROM tblNames;

      The result could be a query or a new table.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      And? What problem are you having?

      The difficult we do right away... ...the impossible takes slightly longer.

      R 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        And? What problem are you having?

        The difficult we do right away... ...the impossible takes slightly longer.

        R Offline
        R Offline
        RC123GUA
        wrote on last edited by
        #3

        I realized that I can use a make-table query to get the data from the middle of the string in one column and make a new table with all the data plus the new column with the data extracted with the mid function. However, the example I gave was just a simple example to make it clear, the problem I am having is the mid function starts at a position counting from the left but the left side of my string before the data I need to extract varies, the right side of the string is always the same, for example, in this string: abcde/fghi/1234567/0005555555-11-000001.txt I need to extract the "0005555555" but the string ahead of it, "/1234567/" is sometimes 6 characters and sometimes 7 characters long.

        Richard Andrew x64R A 2 Replies Last reply
        0
        • R RC123GUA

          I realized that I can use a make-table query to get the data from the middle of the string in one column and make a new table with all the data plus the new column with the data extracted with the mid function. However, the example I gave was just a simple example to make it clear, the problem I am having is the mid function starts at a position counting from the left but the left side of my string before the data I need to extract varies, the right side of the string is always the same, for example, in this string: abcde/fghi/1234567/0005555555-11-000001.txt I need to extract the "0005555555" but the string ahead of it, "/1234567/" is sometimes 6 characters and sometimes 7 characters long.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          I'm not up on all the string functions in MySQL, but it would seem that what you need to do is first split the string according to the "/" delimiter. Look for a string function that will find an occurrence of "/" starting at a certain point in the string, then use that index value to begin your substr function.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • R RC123GUA

            I realized that I can use a make-table query to get the data from the middle of the string in one column and make a new table with all the data plus the new column with the data extracted with the mid function. However, the example I gave was just a simple example to make it clear, the problem I am having is the mid function starts at a position counting from the left but the left side of my string before the data I need to extract varies, the right side of the string is always the same, for example, in this string: abcde/fghi/1234567/0005555555-11-000001.txt I need to extract the "0005555555" but the string ahead of it, "/1234567/" is sometimes 6 characters and sometimes 7 characters long.

            A Offline
            A Offline
            Agecanonix
            wrote on last edited by
            #5

            You can use a negative value for the pos argument (see here[^]). In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning.

            R 1 Reply Last reply
            0
            • A Agecanonix

              You can use a negative value for the pos argument (see here[^]). In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning.

              R Offline
              R Offline
              RC123GUA
              wrote on last edited by
              #6

              Excellent, that is just what I needed, I used the Mid function with a negative number for the start position which counts backward from the end of the string

              SELECT Mid( `AccNum` , -21, 6 )

              Thank you very much for your help.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups