I have created a highscore list, that store results from users after they finish a course. The problem is that if people reload the page, their score gets printed in the database again. A quick fix I did was to only select distinct entries to the highscore list. Unfortunately the duplicate username and score still gets printed in the database.
I tried to remove duplicates using this code
$sql = "INSERT INTO tmp (SELECT DISTINCT * FROM score)";
$sql = "DELETE FROM score";
$sql = "INSERT INTO score (SELECT * FROM tmp)";
Where tmp is a temporary table that stores the entries.
I have also tried to use TRUNCATE on line 2, but its not working.
DISTINCT returns unique rows based on your selection. When you select all of the columns, it will only exclude rows where all of the column values exactly match another row. If there is a unique column such as a primary key, then each row is guaranteed to be unique, and all rows will be returned.
Related
I have this query in php. It's an insert select copying from table2, but I need to get the IDs of the newly created rows and store them into an array. Here is my code:
$sql = "INSERT INTO table1 SELECT distinct * from table2";
$db->query($sql);
I could revert the flow starting with a select on table2 and making all single inserts but it would slow down the script on a big table. Ideas?
You could lock the table, insert the rows, and get the ID of the last item inserted, and then unlock; that way you know that the IDs will be contiguous as no other concurrent user could have changed them. Locking and unlocking is something you want to use with caution though.
An alternative approach could be to use one of the columns in the table - either an 'updated' datetime column, or an insert-id column (for which you put in a value that will be the same across all of your rows.)
That way you can do a subsequent SELECT of the IDs back out of the database matching either the updated time or your chosen insert ID.
I have a PHP array and I want to insert from this array the items that are not already in MySQL table. After searching i could do this item by item using the following code
INSERT INTO `rtusers` (`Status`, `MSISDN`)
SELECT * FROM (SELECT 0, '966111111111') AS tmp
WHERE NOT EXISTS (
SELECT `MSISDN` FROM rtusers
WHERE MSISDN = '966111111111' ) LIMIT 1;
but the problem is i have hundreds of items in the array. if i used this code as a loop this will make hundreds of hits to the database. Does anybody have an easier solution?
You should probably load the array directly into a temporary table of some sort. You can do this with individual insert statements or using load data infile or some other bulk load mechanism.
Then to ignore the record, define a unique index on rtusers(mission) and use:
insert into rtusers(status, mission)
select status, mission
from rtusers_staging
on duplicate key update mission = values(mission);
The on duplicate key part doesn't do anything. It just ignores any duplicate records.
Create a table with new numbers names new_users then execute this query:
INSERT INTO `rtusers` (`Status`, `MSISDN`)
SELECT Status, MSISDN from new_users
WHERE MSISDN NOT IN (SELECT MSISDN FROM rtusers);
This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.
I am trying to insert data into my table such the id field is one more than my previous max id. For example, if I had 21 users registered and another one was added, then that users' id would be 21 (note my ids start at 0).
I tried this:
mysqli_query($con,"INSERT INTO logins (ID,UserName)
VALUES ((SELECT COUNT(ID) FROM logins),'$username')");
This is the error message:
#1093 - You can't specify target table 'logins' for update in FROM clause
I also tried this which works:
$result=mysqli_query($con,"SELECT ID FROM logins WHERE ID=(SELECT MAX(ID) FROM logins)");
while ($db_field = mysqli_fetch_assoc($result))
{
$id=$db_field['ID']+1;
mysqli_query($con,"INSERT INTO logins (ID, UserName)
VALUES ('$id','$username'");
break;
}
But I am looking for a way to do this with one command. Thanks in advance.
I suggest you make id the primary key of the table and enable AUTO_INCREMENT. You can then remove the id column from your query, and increasing id's will be added automatically.
I have a table which has several thousand records.
I want to update all the records which have a duplicate firstname
How can I achieve this with a single query?
Sample table structure:
Fname varchar(100)
Lname varchar(100)
Duplicates int
This duplicate column must be updated with the total number of duplicates with a single query.
Is this possible without running in a loop?
update table as t1
inner join (
select
fname,
count(fname) as total
from table
group by fname) as t2
on t1.fname = t2.fname
set t1.duplicates = t2.total
I have a table which has several thousand records. I want to update all the records which have a duplicate firstname How can I achieve this with a single query?
Are you absolutely sure you want to store the number of the so called duplicates? If not, it's a rather simple query:
SELECT fname, COUNT(1) AS number FROM yourtable GROUP BY fname;
I don't see why you would want to store that number though. What if there's another record inserted? What if there are records deleted? The "number of duplicates" will remain the same, and therefore will become incorrect at the first mutation.
Create the column first, then write a query like:
UPDATE table SET table.duplicates = (SELECT COUNT(*) FROM table r GROUP BY Fname/Lname/some_id)
Maybe this other SO will help?
How do I UPDATE from a SELECT in SQL Server?
You might not be able to do this. You can't update the same table that you are selecting from in the same query.