开发者

mysql: if then else query

开发者 https://www.devze.com 2023-02-08 07:24 出处:网络
I have a table with 3 columns: ID | table | artic开发者_开发知识库le | number 1221 2344 What I need is a query which checks whether a combination of table and article already exists.

I have a table with 3 columns:

ID | table | artic开发者_开发知识库le | number
1     2        2        1  
2     3        4        4

What I need is a query which checks whether a combination of table and article already exists. For example (table 2 and article 2)

In this case number should be incremented by 1.

So it has to like like this now: 1 2 2 2

Otherwise (for example: table 2 and article 5) a new row should be created: 3 2 5 1

Is it possible to do this with 1 statement and how?

Thank you for your help in advance


Check INSERT ... ON DUPLICATE KEY UPDATE Syntax


This may work for you on your DB...

insert into theTable 
(ID, table, article, number) 
values (3, 2, 5, 1) 
where not exists    
(select ID 
from theTable 
where table = 2 and article = 5)
0

精彩评论

暂无评论...
验证码 换一张
取 消