How to compare the ids and insert into database.

  • Iam having these tables

    category

    cat_id (int)

    name(varchar)

    sub_category

    sub_cat_id(int)

    name(varchar)

    category-subcategory-association

    cat_sub_cat_id(int)

    cat_id(int)

    sub_cat_id(int)

    order_master

    order_id(int)

    cat_sub_cat_id(int)

    cat_id(int)

    sub_id(int)

    order_name(varchar)

    These are my tables

    while inserting the data into database if i give cat_id,sub_cat_id it should check first whether this ids are present in the association table or not if the ids are there in the association table then we should get that association id and should insert into the database table of order_master how can i do this can anyone help me.

    Thanks in advance

  • You can add the EXISTS statement to the WHERE clause of the INSERT statement.

    A mock-up:

    INSERT INTO order_master

    SELECT values

    FROM somewhere

    WHERE EXISTS (SELECT 1 FROM category-subcategory-association WHERE cat_id = myCatID and sub_cat_id = mySubCatID);

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • what is mycatId which u have i didnt get that

  • ashalatha.cse76 (6/17/2014)


    what is mycatId which u have i didnt get that

    It's just a placeholder I made up.

    You have to substitute it with your own values/columns/variables...

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • in where class ur comparing the ids in the table but where ur getting the association id

  • $data=array(

    'order_id'=>$this->input->post('order_id'),

    'order_name'=>$this->input->post('order_name'),

    'cat_id'=>$this->input->post('cat_id'),

    'sub_cat_id'=>$this->input->post('sub_cat_id'),

    );

    $this->db->insert('order_master',$data);

    }

    Actually this is my code which i usedto add theorders can u make changes in this how to do that

  • ashalatha.cse76 (6/17/2014)


    in where class ur comparing the ids in the table but where ur getting the association id

    In the

    SELECT values

    FROM somewhere

    part.

    You actually have to fill in something yourself.

    The code I gave was just mock-up code.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • $data=array(

    'order_id'=>$this->input->post('order_id'),

    'order_name'=>$this->input->post('order_name'),

    'cat_id'=>$this->input->post('cat_id'),

    'sub_cat_id'=>$this->input->post('sub_cat_id'),

    );

    $this->db->insert('order_master',$data);

    }

    Actually this is my code which i usedto add theorders can u make changes in this how to do that

    it will be helpfull for me please

  • $data=array(

    'order_id'=>$this->input->post('order_id'),

    'order_name'=>$this->input->post('order_name'),

    'cat_id'=>$this->input->post('cat_id'),

    'sub_cat_id'=>$this->input->post('sub_cat_id'),

    );

    $this->db->insert('order_master',$data);

    }

    Actually this is my code which i usedto add theorders can u make changes in this how to do that

    it will be helpfull for me please

  • ashalatha.cse76 (6/17/2014)


    $data=array(

    'order_id'=>$this->input->post('order_id'),

    'order_name'=>$this->input->post('order_name'),

    'cat_id'=>$this->input->post('cat_id'),

    'sub_cat_id'=>$this->input->post('sub_cat_id'),

    );

    $this->db->insert('order_master',$data);

    }

    Actually this is my code which i usedto add theorders can u make changes in this how to do that

    it will be helpfull for me please

    Nope, sorry. First of all, I don't recognize the language and secondly I only do other peoples job when they pay me.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • k thanks

  • k no problem thanks

  • Hello,

    The code looks like php

    You'll have to construct a query with the where exist clause Koen mentioned.

    Did some googlefu and ended up with:

    insert statements with codeigniter

    query with codeigniter

  • ha it is in php

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply