SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conditional myslq query

    I'm wondering how to set a query to look for something in one table, and if it exists (or doesn't exist) insert a row into another, otherwise do nothing. This is what I've got so far, which works for what I need it to do, but I need some kind of restriction on when this query is run:

    INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
    VALUES ({userid}, 4)

    Basically, I need to look into a table which stores order info, and if a certain item_id exists, then run the query. Otherwise, do nothing.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I didn't understand much from your explanation, but maybe you want to use
    Code:
    INSERT INTO tablename
    
    SELECT
     ....
    FROM tablename
    WHERE ...

  3. #3
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would that be the equivalent of an IF statement? IF a certain value exists in table A, insert a row into table B. IF the value does not exist, do nothing?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    That would insert a row in table B for each row found in table A.

    Why don't you give a more detailed description of what you want? If you find what in table A, what do you want to insert in table B?

  5. #5
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the query i'm trying to put together but since I'm weak with sql, I can't make it work. This probably isn't even the right syntax. I'm trying to put a user into a certain usergroup if a certain product is purchased.

    SELECT product_id, order_id FROM `jos_tienda_orderitems`
    IF (product_id=28)
    THEN
    INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
    VALUES ({userid}, 4)

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Try (didn't test it, and you have to add the correct name of the order table (I assumed the user_id is present in the order table))
    Code:
    INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
    
    SELECT
        user_id
      , 4
    FROM jos_whatever_the_order_table_is_called AS o
    INNER JOIN jos_tienda_orderitems AS oi
    ON oi.order_id = o.order_id
    AND oi.product_id = 28
    WHERE o.user_id = {userid}
    LIMIT 1


Bookmarks

Posting Permissions

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