MySQL: clone rows inside one table [duplicate] - php

This question already has answers here:
mySql copy rows into same table with key value changed (not overwriting existing)
(3 answers)
Closed 4 years ago.
I have one table with the next content
stat_id|stat_type|stat_value
1 |likes |100
1 |reposts |150
Is it possible to clone these rows for another stat_id's in one query?
It's important that we want copies for several new stat_id.

Use INSERT ... SELECT
insert into your_table (c1, c2, ...)
select c1, c2, ...
from your_table
where id = 1

Related

Insert different content IF SQL table contains rows [duplicate]

This question already has answers here:
How to treat MAX() of an empty table as 0 instead of NULL
(3 answers)
SELECT max(x) is returning null; how can I make it return 0?
(7 answers)
Closed 1 year ago.
I am trying to insert different content based on if a SQL table has Rows. So far the Query will check what the max value of sortingOrder is and then + 1. However this query will break if there is no rows in the table. How can I implement a if statement to check if the table has no rows and then if it doesn't set the sortingOrder to '1'.
INSERT INTO faq (question, answer, sortingOrder)
VALUES ('$questionData', '$answerData', (SELECT MAX(sortingOrder) FROM faq C) +1)
Thanks
The best solution is to make sortingOrder an AUTO_INCREMENT column. The database will assign the values automatically, incrementing them for each row.
If you can't do that for some reason, you can check if the subquery returns NULL and replace it with 1.
INSERT INTO faq (question, answer, sortingOrder)
SELECT '$questionData', '$answerData', IFNULL(MAX(sortingOrder)+1, 1)
FROM faq

INSERT using a SELECT statement and also add a variable value to each row of the result at the same time in MySQL [duplicate]

This question already has an answer here:
MySQL insert data with fixed values and multiple select results
(1 answer)
Closed 4 years ago.
I have a table nameValue that has 5 columns (Id,val,firstName,lastName,age). I use a select statement that gives firstName,lastName, age in multiple rows, lets say it returns 5 rows. Id is Auto-Increment. I want to add val to each of the 5 rows returned.
$val=55; INSERT INTO nameValue (val,firstName,lastName,age) VALUES($val, SELECT fName,lName,age FROM nameAge WHERE age<25); something like this where we are insert same variable $val to each of the 5 rows returned by the select statement.
Thats the wrong way. Try this -
INSERT INTO nameValue (val,firstName,lastName,age)
SELECT $val, fName,lName,age FROM nameAge WHERE age < 25

How delete same value in Mysql [duplicate]

This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 6 years ago.
I want to delete same value in topic field and keep first row of value.
such as
no topic
1 1234
2 1234
3 1234
no = autoincrement
output
no topic
1 1234
This my code
$sql ="DELETE FROM data
WHERE no IN (SELECT *
FROM (SELECT no FROM data
GROUP BY topic HAVING (COUNT(*) > 1)
) AS A
)";
This code delete first value but I want to delete all same value and keep first value like example.
try this
DELETE FROM data
WHERE no NOT IN (SELECT no FROM
(SELECT MIN(no) as no,topic FROM data
GROUP BY topic
)NotDelete
);
sqlfiddle

How do I delete duplicate rows in SQL? [duplicate]

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I am trying to delete duplicate rows using SQL, whilst leaving one of the rows behind.
The table I am trying to delete duplicate rows from is called table "A" made up of:
A.AID, A.BID, A.AName, A.AType, A.APrice.
In this table I have a number of duplicate rows with all of the data exactly the same apart from the A.ID.
I am trying to create a query that will look for duplicates and then remove the duplicate making sure one of the rows are left behind. I am using phpMyAdmin and MySQL.
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);
use group by and count
DELETE FROM
YourTable
WHERE
AID NOT IN ( SELECT
MAX(AID)
FROM
YourTable
GROUP BY
BID ,
AName ,
AType ,
APrice );
Consider the query below, this will remove all the duplicate rows and prevents any future duplicate row.
ALTER IGNORE TABLE A ADD UNIQUE INDEX index_name (A.AID, A.BID, A.AName, A.AType, A.APrice );

how to move data from one table to another after some days [duplicate]

This question already has answers here:
How to store day to day records from a mysql table to another?
(2 answers)
Closed 8 years ago.
is it possible to move one table column data to another column data after 1 or 2 days i mean like we have one table which is called table1 another table is table2 table1 have some data like name contact postal address then how to move all of data from table1 selected row to table2 after a period i mean one or two and when table1 data have move to table2 it will be delete from table1 .
in more details like we insert data into table1 like entry_day column using now() we get current date is there any option to after 1 days or 2 days the data will delete from tbl1 and insert into tbl2
$q= "INSERT INTO table1 (name,contact,postal,entrydate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())";
Just create a MySQL event to handle this based on the entrydate. Its quite easy and a quick google search will turn up plenty of information on how to create them.

Categories