Results 1 to 8 of 8

Thread: [RESOLVED] Mysql, is possible to select a column and use that column value in the same select?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    840

    Resolved [RESOLVED] Mysql, is possible to select a column and use that column value in the same select?

    Something like this here.
    SHA2 is a MySQL function.

    I need to get the salt2 value and apply it in the function to create a hash to compare with another hash.

    Code:
    Select salt2, SHA2('A11111111111111111111' + 'salt2', 512) from usertable;
    The above does not work as written as 'salt2' is not the value of the column called salt2
    it runs but with 2 warnings.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,588

    Re: Mysql, is possible to select a column and use that column value in the same selec

    Thread moved from VB.NET to Database Development as this is purely a SQL question and nothing to do with VB.NET.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,588

    Re: Mysql, is possible to select a column and use that column value in the same selec

    If you want the value of the column then can't you just use the value of the column exactly as you did previously?
    Code:
    Select salt2, SHA2('A11111111111111111111' + salt2, 512) from usertable;
    Using the value as an argument to that function is completely unrelated to any other use of that same column in the same SELECT statement.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    840

    Re: Mysql, is possible to select a column and use that column value in the same selec

    I suppose I will have to try nested loops.
    I think that will let me do what I want.
    I need to run a Mysql function inside a reader. Read the function returned value and compare it to the first readers value for priv_aes column.
    https://stackoverflow.com/questions/...-sqldatareader

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,588

    Re: Mysql, is possible to select a column and use that column value in the same selec

    Not sure what any of that has to do with the specific question asked here. Does my suggestion solve that specific problem? If it does then I suggest that you mark this thread Resolved and, if it doesn't, I suggest that you explain what the specific issue is with it. If you have a more complex problem that this was a part of, I suggest creating a separate thread for that and providing a FULL and CLEAR explanation of the problem. It might be possible to do it in a single query, if you provide an explanation of the logic you're trying to implement, including the tables involved and how they are related. If nested loops turns out to be the best option, It may be that populating a DataTable with the first result set or possibly even both is the way to go. We'd need more info to be able to tell.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    840

    Re: Mysql, is possible to select a column and use that column value in the same selec

    concat with the column salt2

    MariaDB [booksgood]> select sha2(concat('A11111111111111111111',salt2), 512) as "Name" from usertable;

    seems to report a matching hash!
    Interesting. Looks like it can be done.

    the hash produced here, matches my stored hash!
    Code:
    MariaDB [booksgood]> select sha2(concat('A11111111111111111111',salt2), 512) as "Name" from usertable;
    +----------------------------------------------------------------------------------------------------------------------------------+
    | Name
               |
    +----------------------------------------------------------------------------------------------------------------------------------+
    | 5d5fd307b6ce0a022b434dbafe62df9f2b43d9194e7ac985845d652efed62a2f5fe7ccfe2283fd7832cb0d87c33a065aa88a4876ffc18b7a8cc19672b4c146c4 |
    +----------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.006 sec)
    
    MariaDB [booksgood]>

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    840

    Re: [RESOLVED] Mysql, is possible to select a column and use that column value in the

    I wont have to do any nested reader. I can do it now with one reader, I think.
    yes, it works. Can also get column values for other columns in same select.
    I had a feeling it could be done.
    I asked on stack, and a guy said concat with the column. So it just came to me to try this.
    https://stackoverflow.com/questions/...mysql-function


    Code:
    MariaDB [booksgood]> select sha2(concat('A11111111111111111111',salt2), 512) as "Name", salt2, myuser from usertable;
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    | Name
               | salt2                | myuser |
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    | 5d5fd307b6ce0a022b434dbafe62df9f2b43d9194e7ac985845d652efed62a2f5fe7ccfe2283fd7832cb0d87c33a065aa88a4876ffc18b7a8cc19672b4c146c4 | 78BC4191BEE98A193312 | New    |
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    1 row in set (0.001 sec)
    
    MariaDB [booksgood]>

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2024
    Posts
    840

    Re: [RESOLVED] Mysql, is possible to select a column and use that column value in the

    Matching set shown here for the hash
    vs2022 is doing a version upgrade, so have to wait to try this out in the program.
    And the kids are coming over, and we are going to go to the city park all morning, and then they will want to play Roblox.
    Likely can't get back to this till the evening.

    One nice thing about posting here, is it prompts my thinking to get things solved. Otherwise, it is like going in blind.
    So, everything helps me to figure things out.

    Code:
    select sha2(concat('A11111111111111111111',salt2), 512) as "Name", salt2, myuser from usertable;
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    | Name
               | salt2                | myuser |
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    | 5d5fd307b6ce0a022b434dbafe62df9f2b43d9194e7ac985845d652efed62a2f5fe7ccfe2283fd7832cb0d87c33a065aa88a4876ffc18b7a8cc19672b4c146c4 | 78BC4191BEE98A193312 | New    |
    +----------------------------------------------------------------------------------------------------------------------------------+----------------------+--------+
    1 row in set (0.001 sec)
    
    
    MariaDB [booksgood]> SELECT SHA2('A1111111111111111111178BC4191BEE98A193312', 512);
    +----------------------------------------------------------------------------------------------------------------------------------+
    | SHA2('A1111111111111111111178BC4191BEE98A193312', 512)                                                                           |
    +----------------------------------------------------------------------------------------------------------------------------------+
    | 5d5fd307b6ce0a022b434dbafe62df9f2b43d9194e7ac985845d652efed62a2f5fe7ccfe2283fd7832cb0d87c33a065aa88a4876ffc18b7a8cc19672b4c146c4 |
    +----------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.001 sec)
    
    MariaDB [booksgood]>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width