MySQL 使用 where 子句将单个值插入到列中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10319169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting a single value into a column with a where clause
提问by Scott Rinebold
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!
我试图在两种情况下将“测试”插入我的 MeetingNotes 列,但对于我的生活,我无法让它工作。是否有可能做到这一点?我是 sql 和 mysql 的初学者?提前致谢!
SELECT MeetingNotes
FROM Meeting
INSERT INTO MeetingNotes
VALUES ('testing')
WHERE MeetingProcessId = '1001' AND MeetingId = '25'
回答by Dan J
You want to use an UPDATE query, which changes values in existing records. An INSERT querystrictly adds new records.
您想使用UPDATE 查询,它会更改现有记录中的值。INSERT 查询严格添加新记录。
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:
为了将来参考,我不确定为什么您的示例中有一个 SELECT 语句:不需要插入或更新记录。将新记录插入会议表(仅给出显示的三列)将如下所示:
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
and1001
in the queries above should be removed
- 由于 INSERT 语句向表中添加了一条全新的记录,因此无法应用按列约束,因此它们不支持 WHERE 子句
- 如果
MeetingId
是数据库生成的自增记录ID,则应该/必须从INSERT语句中排除 - 只有字符串 (CHAR/VARCHAR) 值出现在查询中时才应该被引用,而数字值不应该被引用。因此,例如,如果 MeetingId 和 MeetingProcessId 是整数而不是字符串列,则应删除上述查询周围
25
和1001
中的引号
回答by Eric C.
What you want is probably:
你想要的大概是:
UPDATE Meeting SET MeetingNotes='testing' WHERE MeetingProcessID = '1001' AND MeetingId = '25';