简体   繁体   中英

mySQL trigger - What is wrong with my syntax?

I need to write a trigger where if a customerName of an incoming enquiry matches a customer name in a synonymns table then the customerID from that table is used to find the customer in the customer table.

I got a trigger to work which just searched the customer table but not the synonym table first. I was thinking something like: (It's psuedo-esque code)

CREATE TRIGGER `Find Customer` AFTER INSERT ON `enquiry` FOR EACH ROW
BEGIN
    INSERT INTO customersmatched (enquiryID, customerID)
    SELECT NEW.id, id, customerName, customerID FROM customer, customerSynonyms WHERE
    customerSynonyms.customerName = NEW.companyName AND customer.id = customerSynonyms.customerID
    HAVING COUNT(id)=1
END;

The error I recieve:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 7 

But I'm afriad this doesn't work.

Any idea how one would go about this in mySQL? Thanks!

UPDATE

I tried this way too - it didn't work either!

DELIMITER $$
CREATE TRIGGER Find_Customer AFTER INSERT ON enquiry
FOR EACH ROW
    BEGIN
        IF EXISTS (SELECT customerID FROM customerSynonyms WHERE customerName = NEW.companyName) THEN
            IF EXISTS (SELECT id FROM customer WHERE id = @customerID) THEN
                INSERT INTO customersMatched (enquiryID, customerID)
                HAVING COUNT(id)=1
            END IF;
        END IF;
    END$$
DELIMITER ;

Update 2

It turns out I was WAYYY over complicating things. The customerSynonym table had the customerID stored in a column, which was all I really needed as I could just grab all the data through php with the customerID. The final trigger which works was super simple:

INSERT INTO customersmatched (enquiryID, customerID)
SELECT NEW.id, customerID FROM customersynonyms WHERE
customersynonyms.customerName=NEW.companyName
HAVING COUNT(id)=1

Thanks for all your answers, they're always very much appreciated! :-)

I think you want something like this:

CREATE TRIGGER Find_Customer AFTER INSERT ON enquiry FOR EACH ROW
BEGIN
    INSERT INTO customersmatched (enquiryID, customerID)
    SELECT NEW.id, customerID FROM customer, customerSynonyms 
        WHERE customerSynonyms.customerName = NEW.companyName 
        AND customer.id = customerSynonyms.customerID
        HAVING COUNT(id)=1
END;

A few things to note:

  • I renamed the trigger to Find_Customer . I'm not 100% sure if you can have a space in the trigger name

  • I removed the single quotes around Find_Customer and enquiry. The single quotes could be read as a string literal by mysql and cause problems. It's better to avoid that if possible.

  • I changed your select query to only select NEW.id and customerID, since those are the only two things you're inserting. You might have to play with the select a little bit more to get it exactly how you want it.

  • If this is just part of the trigger, and you have more than just a one-statement trigger, you'll need to set delimiters. Basically, you'd have delimiter | before the trigger definition, and then END| instead of END

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM