0

I am trying to execute the following query:

INSERT INTO table_timesheet (name, datein, dateout)
VALUES ('Rupert', 'NOW()', '')
WHERE NOT EXISTS (
    SELECT name FROM table_listnames WHERE datein='NOW()'
);

But this returns an error.

Basically I don't want to insert a record if the 'datein' field of the record already exists in another record - how to check if the new datein is unique?

And how can i insert in the same way the date out on the same row something like an update the datein row?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167

2 Answers2

3

This INSERT query will do exactly what you want:

INSERT INTO table_timesheet (name, datein, dateout)
SELECT 'Rupert', NOW(), null
FROM DUAL
WHERE NOT EXISTS (
    SELECT * FROM table_listnames WHERE datein=NOW()
);

Please see it here. But a proper solution would be to set a table constraint. How are table_timesheet and table_listnames related? I would use a unique constraint on the datein column, but this depends on what are your requirements.

fthiella
  • 48,073
  • 15
  • 90
  • 106
0

first check this query (using MYSQLI or PDO ) if this query return false than you insert record

IF... this return false

SELECT name FROM table_listnames WHERE datein='NOW()'

FALSE

INSERT INTO table_timesheet (name, datein, dateout)VALUES ('Rupert', 'NOW()', '')
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20