I have a MySQL table with id, username, datetime and entry_id.
I'm logging access to each entry. But I need just one row with same username and entry_id, because I use it just for notifications. How could I programatically delete useless rows? (using PHP and/or SQL)
EDIT:
username is normal string,
ON DUPLICATE KEY wasn't working, because I want to check for duplicity just combination of same username and entry_id. I need just one row, with specific entry_id and username. I want to update just datetime. It's hard to explain :)
So, on each visit is insered new row. I just want datetime to update in every unique username and entry_id combination and not to create always new row.
Thank you for advance!
Just check for a previous entry in the database. If there is one, update it, if there is not an entry - insert one.
After reading your edit it sounds like you want something like:
SELECT username, entry_id FROM <table name> WHERE username = <username> AND entry_id = <entry_id>;
If you want to update the datetime column for that row:
UPDATE <table> SET datetime = <yourdatetime> WHERE username = <username> AND entry_id = <entry_id>
Related
I took a look at many questions similar to mine, but I didn't get what I'm looking for, maybe you guys can help me
I have this table:
What I want to do is:
Insert a new record (regardless whether "user_id" or "course_id" are already exist or not).
BUT!, if there is a record with the same "user_id" and "course_id" and "tutorial_id", then just update "tutorial_id" and "tutorial2_id" and leave the rest as they are.
I don't want to declare column "tutorial_id" as UNIQUE, because more than a user can have the same "tutorial_id" (as you can see in the above picture).
In addition, ON DUPLICATE KEY UPDATE didn't work for me.
I'm thinking of using QUERY two times, one to select and check if record exist, and the other one whether to UPDATE or INSERT, but is that correct?
Use INSERT ... ON DUPLICATE KEY UPDATE ... syntax. For it to work.
If your user_id and course_id combination is unique you could delete the id field from your table and make those two fields a primary key.
In any other case that the id field is also needed and makes a unique combination of the three for each record then make those three fields the primary key for you table (id,user_id and course_id).
How about issuing the UPDATE first, and if no records are affected (using row_count() then INSERT? This way you only test the existence condition once.
rextester demo
update test set tutorial2_id = #tutorial2
where user_id = #user_id and course_id = #course_id and tutorial_id = #tutorial_id;
insert into test (user_id, course_id, tutorial_id, tutorial2_id)
select #user_id, #course_id, #tutorial_id, #tutorial2_id)
where row_count() = 0;
I have two table in which I need to submit data. It is a directory listing website on the first table has the common data name etc but I have to manage business hours as well look below
**Users**
Users_Id Name Age AddressId .........
----+------+------+-----------
**users_hours**
hours_id Users_Id day day day .........
----+------+------+-----------
I have to make a foreign key for this but the only issue is how will I get the id of the user dynamically?
As both of these rows will be created together.
So you want to get the last insert id of Users table, for getting the last id you need to use the mysql_insert_id() here.
First make a insert query for Users and use $userId = mysql_insert_id().
The $userId now hold the last id of the Users table, so you can now make a second query for users_hours.
More about mysql-insert-id.php
I have a field area_code in mysql table by php form. I need the validation & alert when typing the same area code which is already entered and stored in database.
The best way you do is to define a UNIQUE constraint on field area_code on the table.
ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (area_code)
if the code was executed and successful, the server will generate an error if you try to enter area_code that is already present on the table.
You could make a SELECT count statement and check if the returned rows. If so, it means that the record already exists.
SELECT COUNT(id) AS count FROM area_codes WHERE area_code = 'ABC'
If returned row is greater than 1, than the record you are trying to insert already exists.
I am running a insert statement to insert data, but I want to check for any duplicate entries based on date and then do an entry.
All I want is if today a user enters product_name='x', 'x' is unique so that no one can enter product name x again today. But of course the next day they can.
I do not want to run a select before the insert to do the checking. Is there an alternative?
You can either use
1. Insert into... on duplicate update
2. insert.. ignore
This post will answer your question
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
You can use the mysql insert into... on duplicate update syntax which will basically enter in a new row if one isn't there, or if the new row would have caused a key constraint to kick in, then it can be used to update instead.
Lets say you have the following table:
MyTable
ID | Name
1 | Fluffeh
2 | Bobby
3 | Tables
And ID is set as the primary key in the database (meaning it CANNOT have two rows with the same value in it) you would normally try to insert like this:
insert into myTable
values (1, 'Fluffster');
But this would generate an error as there is already a row with ID of 1 in it.
By using the insert on duplicate update the query now looks like this:
insert into myTable
values (1, 'Fluffster')
on duplicate key update Name='Fluffster';
Now, rather than returning an error, it updates the row with the new name instead.
Edit: You can add a unique index across two columns with the following syntax:
ALTER TABLE myTable
ADD UNIQUE INDEX (ID, `name`);
This will now let you use the syntax above to insert rows while having the same ID as other rows, but only if the name is different - or in your case, add the constraint on the varchar and date fields.
Lastly, please do add this sort of information into your question to start with, would have saved everyone a bit of time :)
This question already has answers here:
MySQL 'UPDATE ON DUPLICATE KEY' without a unique column?
(3 answers)
Closed 10 months ago.
I have a table rating with these fields rate_id, game_id, rating, ip. Let suppose that these fields has the following values 1,130,5,155.77.66.55
When a user try to vote for a game, I want with mysql to check if he has already vote for this game so mysql will check if ip and game_id already exists, if they exists then mysql will update the value of rating otherwise will create a new entry.
What is a efficient way to do this?
Create unique index that covers ip + game_id. After that you can use INSERT ... ON DUPLICATE KEY UPDATE statement.
So the total query will be something like
INSERT INTO rating (rate_id, game_id, rating, ip) VALUES (1,130,5,'155.77.66.55')
ON DUPLICATE KEY UPDATE rating = 5
MySQL allows an on duplicate key update syntax for INSERT. So if you set your key to be game_id, user_id (or whichever way you identify the user) then you can use INSERT...on DUPLICATE KEY UPDATE which will do just that:
http://dev.mysql.com/doc/refman/5.5/en/insert.html
You could also take a look at REPLACE INTO. Maybe not for this project but for future reference:
REPLACE works exactly like INSERT,
except that if an old row in the table
has the same value as a new row for a
PRIMARY KEY or a UNIQUE index, the old
row is deleted before the new row is
inserted
from: dev.mysql.com
// check to see if exist
$sql = "SELECT ip FROM rating WHERE ip=$ip && game_id={$game_id}";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if(isset($row['ip'])){
mysql_query("UPDATE HERE");
}else{
mysql_query("INSERT HERE");
}