Results 1 to 14 of 14

Thread: [RESOLVED] Difficult nested SELECT within an INSERT statement

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Resolved [RESOLVED] Difficult nested SELECT within an INSERT statement

    From everything I've read, I don't think this is possible, but here is the statement I am trying to achieve:

    My columns: primary_key, name, name_based_id
    Code:
    INSERT IGNORE INTO artists (primary_key, name, name_based_id) VALUES 
    ('10:w9ftxqe5ldhe', 'Joe', (SELECT COUNT(*) FROM artists WHERE name='Joe')), 
    ('11:jifuxqr5ldh', 'Bob', (SELECT COUNT(*) FROM artists WHERE name='Bob')),
    ('22:a9ftx34gldae', 'Joe', (SELECT COUNT(*) FROM artists WHERE name='Joe')), 
    ...
    Basically, every time I add an artist, I want the name_based_id to be equal to the number of artists already present in the database with that name.

    It would result in something like so:
    '10:w9ftxqe5ldhe', 'Joe', 0
    '11:jifuxqr5ldh', 'Bob', 0
    '22:a9ftx34gldae', 'Joe', 1

    The third row is "Joe 1" because he was the second joe added to the database.

    From what I've read, you can't do a nested SELECT from the same table you're inserting into.

    Is there anyone to accomplish this in one query?

    I'd even settle for multiple queries, as long as it got the job done.

    If I didn't clarify it enough, please let me know. I really need some assistance on this.

    Thanks.

    Note: I'm using MySQL 5.0. I'm working with an InnoDB engine. I'm writing this all in PHP, but I don't think that matters for this question.
    Last edited by eyeRmonkey; Jul 20th, 2007 at 05:11 AM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Difficult nested SELECT within an INSERT statement

    No idea if MySQL supports this type of query but since it works in SQL Server I thought it probably would.

    Code:
    INSERT INTO artists (primary_key, name, name_based_id)
    Select * From
       (Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As [name], name_based_id=(SELECT COUNT(*) FROM artists WHERE name='Joe')
        Union All
        Select '11:jifuxqr5ldh', 'Bob', (SELECT COUNT(*) FROM artists WHERE name='Bob')
        Union All
        Select '22:a9ftx34gldae', 'Joe', (SELECT COUNT(*) FROM artists WHERE name='Joe')
       ) As Data

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    I can't get it to stop having syntax errors. The first error I get is:

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[name], name_based_id=(SELECT COUNT(*) FROM artists WHERE name='Joe')
    Union' at line 3


    When I remove the square brackets from NAME, I then get this error:

    #1054 - Unknown column 'name_based_id' in 'field list'


    Any ideas?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Difficult nested SELECT within an INSERT statement

    To find out what the issue is (I presume a MySQL syntax issue, possibly due to "name_based_id="), try simplified versions, eg:
    Code:
    INSERT INTO artists (primary_key, name, name_based_id)
    Select * From
       (Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As [name], (SELECT COUNT(*) FROM artists WHERE name='Joe') as name_based_id
       ) As Data
    ..if that doesn't work, try this one:
    Code:
    INSERT INTO artists (primary_key, name, name_based_id)
    Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As [name], (SELECT COUNT(*) FROM artists WHERE name='Joe') as name_based_id

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    I still get errors about the square brackets on both of those.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    Doing this query:

    Code:
    INSERT INTO artists (primary_key, name, name_based_id)
    Select * From
       (Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As name, (SELECT COUNT(*) FROM artists WHERE name='Joe') as name_based_id)
    I get this error: #1248 - Every derived table must have its own alias

    I'm clueless... I'm just trying random things to get it to work.

    UPDATE:

    I think you had an extra SELECT in your SQL, si.

    I got this working, now I need to build it up to have all the other two rows in the query.

    Code:
    INSERT INTO artists (primary_key, name, name_based_id)
    Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As name, (SELECT COUNT(*) FROM artists WHERE name='Joe') as name_based_id
    Oh... I just noticed that is what your second query was. Jeeze. Sorry.
    Last edited by eyeRmonkey; Jul 20th, 2007 at 01:47 PM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    Here is what I came up with to do the muti-row insert:

    Code:
    INSERT IGNORE INTO artists (primary_key, name, name_based_id)
    Select '10:w9ftxqe5ldhe' as primary_key, 'Joe' As name, (SELECT COUNT(*) FROM artists WHERE name='Joe') as name_based_id Union All
    Select '11:jifuxqr5ldh', 'Bob', (SELECT COUNT(*) FROM artists WHERE name='Bob') Union All
    Select '22:a9ftx34gldae', 'Joe', (SELECT COUNT(*) FROM artists WHERE name='Joe')
    The problem is, that the name_based_id for the SECOND 'Joe' is still 0. I think that's because the first Joe hasn't been inserted before the nested SELECT COUNT() is evaluated.

    If I add the third row in a separate query from the first two queries, then it is 1 (as it should be). Basically, the only way it works right now is if I never add two people with the same name in a single query.

    So I guess I'm half way to where I want to be. Does anyone have any idea on how I could add two 'Joe's in one query and still have the name_based_id column be accurate?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Difficult nested SELECT within an INSERT statement

    I don't think there is a way of doing that in a single statement, I think you'll need to run two separate ones.

  9. #9

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    Dang. Alright. I guess I'll just do some checking before I execute the query to ensure that no artist name appears twice.

    I'm going to leave this unresolved for a day or so to see if anyone else can think of a way to accomplish that (szlamany or bruce, maybe?).

    Thanks for your help, si.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Difficult nested SELECT within an INSERT statement

    Question - why are you trying to do it this way?

    What if the first Joe is then deleted and you insert another one... then you have two Joes, both with 1 for the field.....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Difficult nested SELECT within an INSERT statement

    I guess I didn't mention that, but the first Joe will never be deleted.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Difficult nested SELECT within an INSERT statement

    I'm going to leave this unresolved for a day or so to see if anyone else can think of a way to accomplish that (szlamany or bruce, maybe?).
    Sorry I can't be of more help as I don't know MySQL. The Insert statement works in SQL Server (including the select count subqueries) so obviously it processes things differently.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] Difficult nested SELECT within an INSERT statement

    It's because the inserts are batched up.... not processed row by row.

    I have a feeling you are going to have to do it one record at a time....

    -tg

    edit: you still never mentioned why you are doing it this way... what's the purpose/significance of the number?
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: [RESOLVED] Difficult nested SELECT within an INSERT statement

    Later on when I use this data, I need to be able a unique identifier based on the name. Therefore, if there are three people with the name 'Joe' in the database, I need to able to make a unique identifier like:

    'Joe_1'
    'Joe_2'
    etc....
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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