SQL statement possible??

Discussion in 'MySQL' started by tua1, May 22, 2008.

  1. #1
    Hello

    I am newbie in SQL and I have a problem, btw sorry about my english, I hope anybody understand me.

    My problem is:

    I have three tables, ex A (cart), B (order), C (order_item). First I fill A table, than I fill B table.

    At last I want to fill C table, I want to put in the table C, pk from B as fk in C (mysql_insert_id()) and in the same time copy 2 fields from table A to C and in the end delete A.

    Is this possible??

    Regards
     
    tua1, May 22, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Yes, this is possible.
     
    mwasif, May 22, 2008 IP
  3. tua1

    tua1 Guest

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your reply.

    In my php script first I add data to order table and with help of mysql_insert_id()
    I add to table order_item, order_id

    for($i=0, i<sum of all items in cart; i++)
    {
    insert into orders_item (order_id)
    VALUE (order_id returned by mysql_inser_id())
    }

    and than I try this

    UPDATE order_item
    SET item_id = ( SELECT item_id
    FROM cart
    WHERE cookie_id=123456781910 limit 1),
    qty= (SELECT qty
    FROM cart
    WHERE cookie_id=123456781910 limit 1)

    WHERE order_id=1 limit 1;

    DELETE FROM cart
    WHERE EXISTS
    ( select item_id, qty
    from order_item
    where cart.item_id = order_item.item.id
    and cart.qty = order_item.qty limit 1)

    And put this statements to for loop (i=0, i<sum of items in cart; i++)
    in the end there is only one item in order_item, when I try to change limit to the sum of items in the cart I have an error: subquery returns more than one row,
    I don't know how to do this with insert

    Could anybody help?
     
    tua1, May 23, 2008 IP
  4. kasapa

    kasapa Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes i agree

    you can use select insert into command

    or making mysql storeprocedure.
     
    kasapa, May 23, 2008 IP