Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following 2 tables(places & Offer (comapny) in mysql database named COUPON1.81

SQL
CREATE TABLE IF NOT EXISTS places (
 id int(60) NOT NULL AUTO_INCREMENT,
 namevarchar(255) NOT NULL,
Description text NOT NULL,
'Company_id' int(60) NOT NULL,
Active int(60) NOT NULL,

CREATE TABLE IF NOT EXISTS offer_company ( 
id int(60) NOT NULL AUTO_INCREMENT,
company_name varchar(150) NOT NULL,
Address text NOT NULL,
'places_id' int(60) NOT NULL,

I need to Update the rows from table company_id to table places when insert a new row on table company


Example, if i insert a new row to Offer_company values as ('1', CSB(pvt)limited, srilanka,123) then the the trigger should to UPDATE the rows from table company_id to table places

Places_id select form places table

places table

+----------+-------------+------+-----+---------+-------+-------+-------+
| id   | name           | Description                   | Company_id  | 
+----------+-------------+------+-----+---------+-------+-------+-------+
| 123  | eiffel tower   |  CSB(pvt)limited              |  0          | 
| 124  | sigiriya       | sigiriya Rock                |  0          | 
+----------+-------------+------+-----+---------+-------+-------+-------+

Offer _comapny table
+----------+-------------+------+-----+---------+-------+
| id   | company_name     | Address      | places_id|
+----------+-------------+------+-----+---------+-------+
| 1    | CSB(pvt)limited |  89/A,Srilanka |  123    | 
+----------+-------------+------+-----+---------+-------+
After select places _id in company table
In Places table, Company_id should be update
places table
+----------+-------------+------+-----+---------+-------+-------+-------+
| id   | name           | Description                   | Company_id  | 
+----------+-------------+------+-----+---------+-------+-------+-------+
| 123  | eiffel tower   |  is an iron lattice tower     |  1          | 
| 124  | sigiriya       | sigiriya Rock                |  0          | 
+----------+-------------+------+-----+---------+-------+-------+-------+

i addedthis trigger
SQL
--
-- Triggers `offer_company`
--
DROP TRIGGER IF EXISTS `add`;
DELIMITER //
CREATE TRIGGER `add` AFTER INSERT ON `offer_company`
 FOR EACH ROW INSERT INTO places (Company_id)
    SELECT new.id
            FROM offer_company
            WHERE offer_company.places_id = new.places_id
//
DELIMITER ;




but this not working how solve this
Posted
Updated 7-Sep-13 5:01am
v2

1 solution

That trigger isn't working because inserting new rows into the places table instead of updating existing ones. The code should be something like:

SQL
--
-- Triggers `offer_company`
--
DROP TRIGGER IF EXISTS `add`;
DELIMITER //
CREATE TRIGGER `add` AFTER INSERT ON `offer_company`
 FOR EACH ROW UPDATE places
    SET company_id = new.id
    WHERE id = new.places_id
//
DELIMITER ;


This of course is problematic for other reasons since if multiple new companies all refer to the same place id then whichever one is inserted last will be the one who's company id is stored.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900