简体   繁体   English

如何用3个键插入或更新mysql表

[英]How to insert or update mysql table with 3 keys

I have a table that looks like this in my mysql: 我的表在mysql中看起来像这样:

location(char17), id(int9), sideid(int9), total(int9), other...(other)

I have to insert into new things into this table ONLY if ID and SideID are not found, and update if they are both the same. 只有在没有找到ID和SideID的情况下,我才需要在此表中插入新内容,并在它们相同的情况下进行更新。 My dilemma is that I need it to check if ID and SideID exist. 我的困境是我需要它来检查ID和SideID是否存在。

So after adding a few things to my table it should look something like this: 因此,在向我的表添加一些内容之后,它应该看起来像这样:

mplace1, 100, 4, 10, other data...
mplace1, 100, 5, 6,  other data...
mplace1, 100, 3, 6,  other data...
mplace1, 50,  3, 6,  other data...
mplace1, 50,  6, 6,  other data...

Those first 3 columns are basically one giant key. 前三列基本上是一个巨大的钥匙。 there can be multiple locations, multiple ID's within a location, and multiple SideID's within it. 可以有多个位置,一个位置中可以有多个ID,并且其中可以有多个SideID。 BUT it cannot have one that matches completely. 但是它不能完全匹配。 So if the Location, ID, and SideID all match, it should not be inserted into the database, but update the "other data". 因此,如果Location,ID和SideID都匹配,则不应将其插入数据库中,而应更新“其他数据”。

Its also really important to have this in one single mysql_query... I've tried for weeks now, and am still lost. 在一个单独的mysql_query中使用它也是非常重要的...我已经尝试了好几周了,但仍然迷路了。 Hope someone has an idea of how to do this. 希望有人对如何做到这一点有所了解。 Note that if it cant be done in one single query, then I'll guess I'll accept that. 请注意,如果不能在单个查询中完成,那么我猜我会接受的。 This is an exceptional task. 这是一项特殊的任务。

Just to clarify. 只是为了澄清。 I mean that those 3 columns(location,id,side) together cant be repeated. 我的意思是那三列(位置,id,侧面)不能一起重复。 So the other columns will be able to have different values. 因此,其他列将可以具有不同的值。

So if mplace1, 100, 4, 10, "Some other data 1" was in the table. 因此,如果表中有mplace1、100、4、10,则“其他一些数据1” inserting this mplace1, 100, 4, 999, "Some other" would update. 插入此mplace1、100、4、999,“其他”将会更新。 How does one specify what columns to check and what doesn't matter. 如何指定要检查的列和无关紧要的列。

INSERT OR REPLACE INTO table (column1, column2) VALUES ('value1', 'value2');

这将自动检查重复项(例如,重复的ID),因此如果需要,它会update否则只会insert新行。

INSERT OR REPLACE INTO table (column1, column2) VALUES ('value1', 'value2');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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