Am having two tables inside my Database lets say Table1 and Table2, and am trying to copy the values from a specific rows(fields) in Table2 to a specific single row on Table1, The specific rows to be moved are determined by the User Input of Particular UserID of each row from Table1.
Table Structure:
Table 1: a_uid,a_FName,a_Username,a_PhoneNo,b_uid,b_FName,b_Username,b_PhoneNo......
And Table 2 Structure:
Table 2: uid,FName,Username,PhoneNo.....
Am uing the INSERT INTO .. SELECT statement, but with multiple WHERE clause but its giving me erros
INSERT INTO table1 WHERE uid='userinput1' (b_FName,b_Username,b_PhoneNo,) SELECT FName,Username,PhoneNo FROM table2 WHERE uid='userinput2';
But am getting Error
This type of clause was previously parsed. (near WHERE)
If the row in table1 already exists, you need an UPDATE .. JOIN statement instead of INSERT .. SELECT.
UPDATE table1 t1
JOIN table2 t2 ON t2.uid='userinput2'
SET t1.b_FName = t2.FName,
t1.b_Username = t2.Username,
t1.b_PhoneNo = t2.PhoneNo
WHERE t1.uid='userinput1'
If you don't know if the row in table1 already exists, you can use an INSERT .. SELECT .. ON DUPLICATE KEY UPDATE statement:
INSERT INTO table1 (uid, b_FName, b_Username, b_PhoneNo)
SELECT 'userinput1', FName, Username, PhoneNo
FROM table2
WHERE uid = 'userinput2'
ON DUPLICATE KEY UPDATE
SET b_FName = VALUES(FName),
b_Username = VALUES(Username),
b_PhoneNo = VALUES(PhoneNo)
Note that uid sould be primary keys or at least unique in both tables.
For sql-server it would be something like this:
INSERT INTO table1 (uid,FName,Username,PhoneNo)
SELECT #userinput1, FName,Username,PhoneNo
FROM table2
WHERE table2.uid=#userinput2;
Related
I wanted to insert two values, one is filled by a fixed number and the other one is the id from another table.
Now I got the error
#1242 - Subquery returns more than 1 row.
INSERT INTO table1 (value1, value2) VALUES
(6 , (SELECT id FROM table2 WHERE name = 'Peter'))
Maybe you can help me.
If you want to insert record into your table1 for every record in table2 where name is Peter this approach should work.
This insert query will insert all records from table2 where name is "Peter" into table1. If you want to insert only one record you could use LIMIT as Macmee has explained in his answer
insert into dbo.table1
(
value1,
value2
)(
select
6,
table2.id
from
table2
where
name = 'Peter'
)
try using LIMIT 1:
INSERT INTO table1 (value1, value2) VALUES (6 , (SELECT id FROM table2 WHERE name = 'Peter' LIMIT 1))
This way in your nested query (SELECT id FROM table2 WHERE name = 'Peter' LIMIT 1) it will only return the first match, and your insert should go through.
Keep in mind that if your intent was to insert new rows for EVERY row in table2 who's name is "Peter" then this will only insert the first one.
I have 2 tables let say table1 & table2
table1 contains uniqueId, name1, name2, value fields
table2 contains id, uniqueName, keywords fields
table2.keyworks have comma separate names.
So, what I am trying to do is below.
select * from table1
//1> replace table1.name1 with table2.uniqueName if table2.keywords has
//table1.name1
//2> replace table1.name2 with table2.uniqueName if table2.keywords has
//table1.name2
select *,(case when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name1
when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name2 end)from table1
Try this.
I have 2 tables:
Table1
Table2
When I move a row from table1 to table2, I also want to update the datetime field and 1 more field.
Say both table have identical column like this:
id
shipped_by
datetime
other_column
I have the following sql line, but it is not working of course. But I want to have it something like that.
$query = "INSERT INTO table2
SELECT * FROM table1
WHERE id = '$id' UPDATE table2
SET shipped_by='$shipped_by', datetime='$datetime'";
The variable $shipped_by selects the userid, and $datetime date from now.
Can anyone help me with this sql code to make it work? I cannot figure it out.
Thank you.
To insert data form table1 with some column data modified can be done with insert and select without update.. select * should be used here, each column must be listed except for modified ones..
$query = "INSERT INTO table2
SELECT id, '$shipped_by', '$datetime', other_column FROM table1
WHERE id = '$id'";
I am stuck to update one column of table by comparing with another table in php/Mysql. I have tried to speed up the process by indexing the table columns, optimizing the query etc but unable to speed up the process.
In my php based application there is two table (table A and table B) , I want to update one column of table A by comparing with table B (with two column - name & sku).
Previously above process has taken max 15 mints to update 28k products. But now both table (table A and table B) have 60k rows. Now it's taking more than two hours. I have used below query
mysql_query("UPDATE tableA a
JOIN tableB b ON a.product_code_sku = b.sku
SET a.is_existing_product = '1'") or die(mysql_error());
mysql_query("UPDATE tableA a
JOIN tableB b ON a.product_name = b.product_name
SET a.is_existing_product = '1'") or die(mysql_error());
Above query was very slow after that I have changed the updating process like below
$query_result = mysql_query("SELECT t1.`id`,t2.`product_id` FROM `tableA` t1,
`tableB` t2 where (t1.product_code_sku = t2.sku
or t1.product_name = t2.product_name)") or die (mysql_error());
while($result_row = mysql_fetch_array($query_result))
{
mysql_query("UPDATE `tableA` SET is_existing_product = '1'
where id = '".$result_row['id']."' ") or die (mysql_error());
}
But all of my efforts are in vain.
Please advice me how to make the process faster.
Your first update query and the second update query is doing two different thing. The second query is slower because you are using a OR for comparison.
You can consider to create a temporary table to compare and insert, the update back to tableA.
First and all, you should examine the execution for the two join queries, like
desc select a.id
from tableA a
join tableB b ON a.product_code_sku = b.sku;
If this is the reason why the update is slow, you should optimize the query.
Otherwise, you can try the below:
For instance (assuming ID the primary key),
// make sure the columns are in the same data type
create table tmp_sku (
id .. // just the primary key, make sure is using the same data type as in tableA
);
// do a insert into this temporary table
insert into tmp_sku select a.id
from tableA a
join tableB b ON a.product_code_sku = b.sku;
// now we have list of matches,
// then do a insert .. duplicate key update
// by comparing the primary id
insert into tableA (id, is_existing_product)
select tmp_sku.id, 1 from tmp_sku
on duplicate key set is_existing_product = 1;
// repeat for the product name
truncate tmp_sku;
insert into tmp_sku
select a.id
from tableA a
join tableB b ON a.product_name = b.product_name;
// repeat the duplicate .. update
insert into tableA (id, is_existing_product)
select tmp_sku.id, 1 from tmp_sku
on duplicate key set is_existing_product = 1;
I have a mysql table called jos_users_quizzes with the following columns:
id
quiz_id
user_id
I have a second table called jos_users with this columns
id
name
username
department
the user_id on first table is linked with the id of second table so
quiz_id = id (jos_users)
How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... in one click
I am thinking meabe a sub query or a loop, but no sure how.
Thanks in advance!
INSERT jos_users_quizzes (quiz_id, user_id)
SELECT $quizID, id
FROM jos_users
WHERE department = 'selected department'
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example
INSERT INTO jos_users_quizzes (quiz_id)
SELECT jos_users.id
FROM jos_users WHERE jos_users.department = 100;