Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a single value into a column with a where clause

Tags:

sql

mysql

Im trying to insert 'testing' into my MeetingNotes column under two conditions but for the life of me I cannot get it to work. Is it possible to do this? I am a beginner with sql and mysql? Thanks in advance!

SELECT MeetingNotes
FROM Meeting
INSERT INTO MeetingNotes
VALUES ('testing')
WHERE MeetingProcessId = '1001' AND MeetingId = '25'
like image 306
Scott Rinebold Avatar asked Apr 25 '12 15:04

Scott Rinebold


2 Answers

You want to use an UPDATE query, which changes values in existing records. An INSERT query strictly adds new records.

UPDATE Meeting
SET MeetingNotes = 'testing'
WHERE MeetingProcessId = '1001' AND MeetingId = '25'

For future reference, I'm not sure why you have a SELECT statement in your example: it isn't needed to insert or update records. Inserting a new record into the Meeting table (given only the three columns shown) would look like this:

INSERT INTO Meeting (MeetingId, MeetingProcessId, MeetingNotes)
VALUES ('25', '1001', 'Notes about this very exciting meeting...')

A couple notes on this:

  • Since INSERT statements add an entirely new record to the table, columnwise constraints can't be applied, so they don't support a WHERE clause
  • If MeetingId is an auto-incrementing record ID generated by the database, it should be / must be left out of INSERT statements
  • Only string (CHAR/VARCHAR) values should be quoted when they appear in queries, numeric values should not. So if, for example, MeetingId and MeetingProcessId are integer instead of string columns, the quote-marks around 25 and 1001 in the queries above should be removed
like image 121
Dan J Avatar answered Oct 20 '22 17:10

Dan J


What you want is probably:

UPDATE Meeting SET MeetingNotes='testing' WHERE MeetingProcessID = '1001' AND MeetingId = '25';
like image 41
Eric C. Avatar answered Oct 20 '22 19:10

Eric C.