Couldn't find exactly what I'm looking for anywhere else.
So I have table 1
users
----------
id
username
password
bio
isuser
email
and table 2
wp_users
----------
id
user_login
nice_username
password
email
There are 500 rows in 'wp_users' table. I would like to copy 'id' and 'user_login' into the users table ('id' and 'username') for each row.
How can I do this? MySQL isn't my strong point lol.
UPDATE: I have updated the tables above as I tried to simplify it but in return got the wrong solution.
You can use an INSERT - SELECT statement:
INSERT INTO `users` (id,username,password,bio,isuser,email) SELECT id,
user_login,null,null,null,null FROM `wp_users`;
You can put another fields or even static data on each field of the SELECT part. I've put nulls just to illustrate.
Just remember that anything the SELECT fetches will be inserted in the table the INSERT statement says (so you can use clauses like WHERE, GROUP BY, HAVING, etc on the SELECT part).
I believe you are looking for a simple insert statement:
INSERT INTO users (id,username)
SELECT W.id,
W.user_login
FROM wp_users W
WHERE NOT EXISTS (SELECT NULL
FROM users U
WHERE U.ID = W.ID);
I am assuming the other columns in your users table are nullable, if not, you can add more columns to the select statement with the values in you'd like for the other columns.
You haven't mentioned any other keys or constraints, so I have assumed there aren't any.
I have also provided a check in the WHERE clause to see if a row already exists for the same ID, so you only insert a row for each ID once.
Related
I am trying to combine INSERT, UPDATE and WHERE NOT EXISTS in one and the same query.
What I have at the moment is these two queries that works as expected separately
INSERT INTO settings (mid) SELECT '123' FROM DUAL WHERE NOT EXISTS (SELECT mid FROM settings WHERE mid='123');
UPDATE settings SET vote = CONCAT_WS(',', vote, '22') WHERE mid = '123'
What I am trying to achieve is combine them together, so I can bother the db once
What I have is a table with two columns: mid that stores the unique user id, that column is also a primary, and another column that is called vote that stores the user votes in a comma separated order.
So, my aim here is to first check is the user is having a row already created for him (if not to create it) and then if the row exists to add the new vote 22 in my example to the list.
So I have the following hierarchical database structure:
Table person has columns id and some other fields.
Table car has columns id, owner (with a foreign key constraint to person.id) and some other field
Table bumpersticker has columns id, car (with a foreign key constraint to car.id) and some other fields
I want to INSERT a row in to bumpersticker and have values to populate the row. I also have a person.id value of the person trying to add the bumpersticker.
What is the best practice to ensure that the car.owner value selected from the bumpersticker.car is in fact the same person.id as I have?
I guess one obvious way is to first execute a select query, on the car table and select the car.owner and validate that this value is the same value as the id of the person trying to add the bumpersticker and then execute an insert query.
but this seems like something there must be an elegant solution to in MySQL. at least not having to do two separate queries.
Most thankful for your help!
You can insert from a SELECT query that tests if the owner matches the criteria
INSERT INTO bumpersticker (car, sticker_text)
SELECT c.id, "If you can read this you're too close"
FROM car AS c
WHERE c.id = #car_id AND c.owner = #person_id
#car_id is the ID of the car you're adding the bumpersticker for, and #person_id is the ID of the user doing the insert. If the owner ID doesn't match, the SELECT query will return no rows, so nothing gets inserted.
DEMO
I have a table (People) with columns (id, name, year). Is there any way I can get all the ids ( SELECT id FROM people ) and use them for creating new rows in other table (people_id, additional_info). I mean that for every id in table People I want to add new row in other table containing the id and some additional information. Maybe something like for-loop is needed here ?
Well, usually you have information on a person and write one record with this information. It makes litte sense to write empty records for all persons. Moreover, when it's just one record per person, why not simply add an information column to the people table?
Anyway, you can create mock records thus:
insert into people_info (people_id, additional_info)
select id, null from people;
Insert into targetTable (id, additional_info)
select id,'additionalinfo' from PEOPLE
No for loop is needed, Just use the above query.
You can use INSERT ... SELECT syntax for MySQL to insert the rows into people_info table (documentation here).
e.g.
insert into people_info(...)
select (...) from people; <and posibly other tables>
This should be a pretty simple thing, but I'm quite new to (My)SQL.
Basically, given a customers table with attributes id, client, etc., where the client field is not necessarily unique, I want to eliminate rows where the client field is a duplicate of a previous value.
The following:
SELECT MIN(id) FROM customers GROUP BY client
returns the unique id's of the rows I want. I want everything else out.
I tried
DELETE FROM customers WHERE customer.id NOT IN
(SELECT MAX(id) FROM customers GROUP BY client)
to no avail. (ERROR 1093 (HY000): You can't specify target table 'customers' for update in FROM clause).
Why doesn't it work and what do I need to do to accomplish my goal?
Thank you.
You could create a temporary table that holds the values you want to remove. Then your delete query could be based on that temporary table.
I'm using MySQL 4.1. Some tables have duplicates entries that go against the constraints.
When I try to group rows, MySQL doesn't recognise the rows as being similar.
Example:
Table A has a column "Name" with the Unique proprety.
The table contains one row with the name 'Hach?' and one row with the same name but a square at the end instead of the '?' (which I can't reproduce in this textfield)
A "Group by" on these 2 rows return 2 separate rows
This cause several problems including the fact that I can't export and reimport the database. On reimporting an error mentions that a Insert has failed because it violates a constraint.
In theory I could try to import, wait for the first error, fix the import script and the original DB, and repeat. In pratice, that would take forever.
Is there a way to list all the anomalies or force the database to recheck constraints (and list all the values/rows that go against them) ?
I can supply the .MYD file if it can be helpful.
To list all the anomalies:
SELECT name, count(*) FROM TableA GROUP BY name HAVING count(*) > 1;
There are a few ways to tackle deleting the dups and your path will depend heavily on the number of dups you have.
See this SO question for ways of removing those from your table.
Here is the solution I provided there:
-- Setup for example
create table people (fname varchar(10), lname varchar(10));
insert into people values ('Bob', 'Newhart');
insert into people values ('Bob', 'Newhart');
insert into people values ('Bill', 'Cosby');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Adam', 'Sandler');
-- Show table with duplicates
select * from people;
-- Create table with one version of each duplicate record
create table dups as
select distinct fname, lname, count(*)
from people group by fname, lname
having count(*) > 1;
-- Delete all matching duplicate records
delete people from people inner join dups
on people.fname = dups.fname AND
people.lname = dups.lname;
-- Insert single record of each dup back into table
insert into people select fname, lname from dups;
-- Show Fixed table
select * from people;
Create a new table, select all rows and group by the unique key (in the example column name) and insert in the new table.
To find out what is that character, do the following query:
SELECT HEX(Name) FROM TableName WHERE Name LIKE 'Hach%'
You will se the ascii code of that 'square'.
If that character is 'x', you could update like this:(but if that column is Unique you will have some errors)
UPDATE TableName SET Name=TRIM(TRAILING 'x' FROM Name);
I'll assume this is a MySQL 4.1 random bug. Somes values are just changing on their own for no particular reason even if they violates some MySQL constraints. MySQL is simply ignoring those violations.
To solve my problem, I will write a prog that tries to resinsert every line of data in the same table (to be precise : another table with the same caracteristics) and log every instance of failures.
I will leave the incident open for a while in case someone gets the same problem and someone else finds a more practical solution.