This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove Duplicate Rows Leaving Oldest Row Only?
MySQL remove duplicate rows
Say that I have the following table coolumns: int logicalid(pk) ,int home, int person say the I have the following records...
1,5,6
2,5,6
3,5,5
4,5,5
After my query, I want to place into the new table only one row with the same home,person column values so this will be the output result:
1,5,6
2,5,5
Any ideas??
Create your new table with an auto-increment column for the id.
Then insert into the table, using a query such as:
insert into newTable(home, person)
select distinct home, person
from oldTable
INSERT INTO newtable(home, person) SELECT DISTINCT home, person FROM sourcetable
Related
This question already has an answer here:
MySQL - insert data from another table merged with constants
(1 answer)
Closed 5 months ago.
For example i have a table name Info have 3 columns ID,COST,CITY
INSERT INTO Info
SELECT *
FROM Info;
WHERE Cost = 100
This will Create exact same copy of 1st row.
I want that new entry need to be 1,250,pune
How can I do when i have 100 columns and need to change only 5 columns thats the real deal?
You can simply add the new value into the select:
INSERT INTO Info (Cost, city)
SELECT 250,city
FROM Info
WHERE Cost = 100
But the ID can not be copied, because an id should be unique
If your ID isn't an auto_increment you can add the id to the queries:
INSERT INTO Info (ID, Cost, city)
SELECT ID,250,city
FROM Info
WHERE Cost = 100
This question already has answers here:
MYSQL - add new column with default string concat with data from existing column
(2 answers)
String concatenation in MySQL
(5 answers)
Closed 11 months ago.
For Example
I have the following Columns in a DB-table: Name, Surname, Adress, Contact.
In one row the value for the column name is Smith , the surname Bob and the adress Street1.
How can i combine the mentioned values to the column Contact?
The Value in this row should be like this then:
Bob_Smith_Street1
I want this to be automatically created in the table. so as soon as I right click on the Table -->select 1000 rows, the value is displayed. Without having me to enter the mentioned select command. Is this possible? Can anything be done when creating the table for this. So for example create table employees (Contact(name""surname""address)).
thanks
ps. sorry for my bad english:)
Use CONCAT(). For example:
select concat(Name, '_', Surname, '_', Adress) as contact from t
This question already has answers here:
Delete sql rows where IDs do not have a match from another table
(4 answers)
Closed 1 year ago.
I have a MySQL database where i want to clean up a table but i do not know how to compare against other table entrys and hope someone can help.
One table in the database is called "stats". in the table "Stats" there is a row called "Page_title" which holds an ID number. The same ID number should exist in the table "url,id" otherwise that entry from "stats" should be removed, how can this be done?
Please see this image for reference.
You can do this with a sub select:
delete from stats where page_title not in (select id from shorturl)
This question already has answers here:
Insert into a MySQL table or update if exists
(12 answers)
INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE
(4 answers)
Closed 5 years ago.
How to insert values from one table to another table if that particular value doesn't exists and if exists update value of one column of that row.
I am trying to find distinct rows from one table and store them in another table.
But if that value already exists in the new Table, then i want to add 1 to the count column (eg:: if 1, then 1+1=2).. and if doesn't exists the insert in a row.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table with two attributes
Staffnumber Name
Now I want to add a new staff member into this table, for example: Christian, and Staffnumber is the current max Staffnumber + 1. (Assume I have til now 20 staff members, then Christian would be number 21)
How could i do that, without knowing how many staff members I had? Is there any way to make SQL numbers the Christian as 21?
Making a call to the table to count the records should achieve sort of what you're looking for. Forgive my syntax but it should be something along these lines...
INSERT INTO TABLE_NAME (STAFF_NUMBER, NAME) VALUES ((SELECT COUNT(*) FROM TABLE_NAME) + 1, "Christian");
Keep in mind if you delete staff members and add more later this won't work because the count will be off.
If you make your STAFF_NUMBER column a primary key, you shouldn't have to specify this value at all when entering new values, it should auto-increment and assign a random ID to the staff member.
INSERT INTO TABLE_NAME (NAME) VALUES ("Christian");
If there is no auto-increment on your column, then you would just do a simple INSERT statement:
INSERT INTO table_name (Staffnumber, Name)
VALUES (21, 'Christian')
Also here is a link to set AUTO_INCREMENT for your column for MySQL:
http://www.w3schools.com/sql/sql_autoincrement.asp
Increment
So if you don't have auto increment. You will want to select the COUNT() from Staffnumber.
SELECT COUNT(*) + 1 From Table;
Inserting
Now you also want to insert a new record into the table.
INSERT INTO Customers VALUES(1,"Christian");
But the problem with this is that it will only Staffnumber of 1. So you need to combine both of these.
Final
INSERT INTO Table VALUES((SELECT COUNT(*)+1 FROM Table),"Christian");
INSERT INTO Table VALUES((SELECT COUNT(*) FROM Table) + 1,"Christian");