MySQL IF INSERT or UPDATE

395
March 25, 2017, at 03:12 AM

I have the following query:

IF (some statement) THEN INSERT ... ELSE UPDATE ...

My question is how can I check which action took place? Let's say I want to print 1 if it was an INSERT or 2 if UPDATE using php.

Thank you,

Answer 1

If you don't have one already, create a column that is populated on insert and not changed during updates (create_time would be a suggestion)

then... after insert, compare the time difference with the current time... if the time difference is within a few seconds (or whatever threshold seems best to you) then the operation was an insert, otherwise it was an update

select if( timestampdiff( second, create_time, now() ) <= 3, 1, 2) from...
Rent Charter Buses Company
READ ALSO
Selecting from multiple tables in C#

Selecting from multiple tables in C#

I am trying to select data from multiple MySQL tables in one simple statement, my problem however is when I try to run it I get the following error:

356
MySQL database query using multiple table JOIN

MySQL database query using multiple table JOIN

I'm using a MySQL non normalized database with duplicate records

288
delete rows with no consecutive numbers [on hold]

delete rows with no consecutive numbers [on hold]

Hi I have a database with odds and evens and i need to delete all rows that do not contain any consecutive numbers, for example row id 3 needs to be deleted and keep rows id 1, 2 and 4

311
MYSQL- login Warning: Using a password on the command line interface can be insecure

MYSQL- login Warning: Using a password on the command line interface can be insecure

-bash-42$ mysql -u root -pmypassword Warning: Using a password on the command line interface can be insecure

517