Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7898295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:23:40+00:00 2026-06-03T08:23:40+00:00

Question: What is the correct syntax to use when selecting a column value into

  • 0

Question:

What is the correct syntax to use when selecting a column value into an UPDATE/INSERT trigger NEW variable?

Details:

Here are the [simplified] tables I am using as well as the query. For the purposes of this example I have chosen something that is recursive, in this case a comment (ie. one can comment on a picture, but also on the comment). At any given time I want to know what picture a comment is commenting on, but I don’t want to query the table n times to get to that picture (hence why I am rejecting the Adjacency List Model), so I have a trigger on insert to copy the target picture over.

PICTURES table:

CREATE TABLE IF NOT EXISTS pictures
(
   id INT NOT NULL AUTO_INCREMENT,
   PRIMARY KEY ( id )
)

COMMENTS table:

CREATE TABLE IF NOT EXISTS comments
(
   id INT NOT NULL AUTO_INCREMENT,
   picture_id INT NOT NULL,
   comment_id INT NOT NULL,
   PRIMARY KEY ( id ),
   FOREIGN KEY ( picture_id ) REFERENCES pictures ( id ),
   FOREIGN KEY ( comment_id ) REFERENCES comments ( id )
)

COMMENTS trigger: Note that NEW.comment_id and NEW.picture_id are NOT NULL, therefore, they default to zero (FALSE). In my actual program I have an XOR to reject invalid attempts to assign both a picture and a comment id.

CREATE TRIGGER bi_comments_fer BEFORE INSERT ON comments FOR EACH ROW
BEGIN
   IF ( NEW.comment_id )
   THEN
      select NEW.picture_id := picture_id from comments where id = NEW.comment_id;
   END IF;
END

I tried including an @ sign, like so

select @NEW.picture_id := picture_id from comments where id = NEW.comment_id;

But that also raised a syntax error. Can anyone tell me how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:23:41+00:00Added an answer on June 3, 2026 at 8:23 am
    SET NEW.picture_id := (SELECT picture_id FROM comments WHERE id = NEW.commend_id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, I've one question to ask. Which one is the correct syntax in jquery
Sorry if the question isn't correct, I'm very new in Objective-C. I understand why
EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM
A light-weight question for the experts. I can't seem to figure the correct syntax
I have a problem with the correct syntax to use UNION and GROUP_CONCAT in
If my understanding of deep and shallow copying is correct my question is an
This is something of an extension to this question: Dispatching to correct function with
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
This is a simple question: Is this a correct way to get an integer
Im Making a question for a website and would like the the correct /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.