Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    419
    Thanks
    18
    Thanked 2 Times in 2 Posts

    combine a query with an insert?

    I posted on a related issue in the php forum, then I thought I would as a piece of the question here.

    I have two pieces that I'm wondering how to combine into one complete query..

    First I query table1 to get the id for several items, found by 2 or 3 columns.
    Then I insert a same value into table2 for all those queried id's.

    Query one:
    Code:
    SELECT DISTINCT  item_id
    FROM table1
    WHERE taxonomic_genus = 'Acer' AND scientific_name = 'platanoides' AND infraspecific_epithet = ''
    OR taxonomic_genus = 'Alhagi' AND scientific_name = 'maurorum' AND infraspecific_epithet = ''
    /*plus another 1000+ or lines after this */

    then, once I've got the ids I build this to add the value 423 for each one

    Code:
    INSERT INTO table2(item_id, term_id,high_low,male_female) 
    VALUES 
    (100005,423,null,null),
    (21405,423,null,null),
    (103505,423,null,null),
    /* etc for another 1000+ lines */
    those are the two mysql statements I'm hoping to combine.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    419
    Thanks
    18
    Thanked 2 Times in 2 Posts
    damn... I think I answered my own question. I didn't realize it was that easy to nest a query.


    PHP Code:

    INSERT INTO table2
    (item_idterm_id,high_low,male_female
    VALUES 
    (
    (
    SELECT DISTINCT item_id
    FROM table1
    WHERE taxonomic_genus 
    'Lavatera' AND scientific_name 'venosa' AND infraspecific_epithet '')
    ,
    423,null,null); 


  •  

    Posting Permissions

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