mysql- Update query with respect to select query [duplicate] - mysql

This question already has answers here:
mysql update column with value from another table
(9 answers)
Update mysql table with data from another table
(5 answers)
Closed 5 years ago.
hi here i have two tables named as test and test2.
test is as follows
test2 is as follows
i am using below sql code to get the below output.
UPDATE `test`
SET `availability` = 'ok'
WHERE
`id` = '(SELECT
test.id
FROM
test2
INNER JOIN test ON test.id = test2.PId)';
I requires below output. but it outcomes no any output. kindly help. any mistakes done by my end or if there is any best mothod to get below output, kindly mention

I think you're looking for something along the lines of
UPDATE test
INNER JOIN test2 on test.id = test2.PId
SET test.availability = 'OK'

Sounds like you don't need an update, just a query with a join:
SELECT test.id, test.name, CASE WHEN teste2.pid IS NOT NULL THEN 'OK' END
FROM test
JOIN test2 ON test.id = test2.pid

I am not sure if this will work.but hey we all try here right..
UPDATE `test`
SET `availability` = 'ok'
WHERE
`id` in '(SELECT
PId from test2)';
I don't get , why do you need inner join.You only need those id's that are present in test2 table,then just take it.
although i have used 'in' keyword,try with '=' also but i doubt it will work because inner query is returning a list of id's.thanks.

Related

MySql : Updating all except the first row [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I went through the previous answer like this, but it gives me the following error : You can't specify target table 'table_name' for update in FROM clause.
I have a table with say 3 columns (id -> auto increment primary id) :
id, roll_no and attendance
And for selected roll numbers having many entries each, except the first entry I want to update all entry attendance field as P.
The query which I wrote is following :
UPDATE tbl_class_attendance
set attendance = 'P'
where id NOT IN
(Select min(id)
from tbl_class_attendance
WHERE roll_no IN ('25', '45', '55')
GROUP
BY roll_no;
But it gives me the above error.
I also went through other answers asking to use two select queries but there the answer I didn't find completely easy to understand as well as difficulty in executing for my selected list of roll numbers.
So, is there a way to update?
EDIT : Answer given below
UPDATE tbl_class_attendance t1
LEFT JOIN ( SELECT t2.roll_no, MIN(t2.id) id
FROM tbl_class_attendance t2
WHERE t2.roll_no IN ('25', '45', '55')
GROUP BY t2.roll_no ) t3 USING (roll_no, id)
SET t1.attendance = 'P'
WHERE t3.id IS NULL;
Got it working by following :
Update table SET a=value WHERE x IN
(Select * from (select x from table where condition) as t)
Credit : https://stackoverflow.com/a/43610081/6366458

Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement

I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);

Error updating record with sub query [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I am updating record on the base of sub query but it giving me error
you can't specify target table for update in from clause
my query is
UPDATE paymentinfo set customer_id =
(
SELECT transation.transactionid
FROM paymenttransaction AS transation
LEFT JOIN paymentinfo as payment
ON (transation.paymentinfoid=payment.paymentinfoid)
where payment.hash="0b576d33c57484692131471a847eab7c"
)
WHERE hash="0b576d33c57484692131471a847eab7c"
where am i wrong and what will be perfect solution for that problem
You are updating the table 'paymentinfo' also at same time you are using this table for selection in subquery .
Please break this query in two parts and it will work .
I think it is simplest (in your case) to use the double subquery method:
UPDATE paymentinfo
SET customer_id = (SELECT transactionid
FROM (SELECT t.transactionid
FROM paymenttransaction pt LEFT JOIN
paymentinfo pi
ON t.paymentinfoid = pi.paymentinfoid
WHERE p.hash = '0b576d33c57484692131471a847eab7c'
) t
)
WHERE hash = '0b576d33c57484692131471a847eab7c';
Usually, you want to switch these to use JOIN, but I think that is a bit complicated in this case.

Append text from one table to another table

Got a small question in which I can't wrap my head around..
I have 2 tables, 1 table where there is a Youtube Link i need to append to another column in table 2. But not every record has a Youtube link, which means I need to check if in both tables the names are the same (WHERE name1 = name2?) But how can I append text to already existing text in table 2? Something like this?
UPDATE table2 SET text2 = (text2 + '/n' + table1.text1) WHERE name1 = name2?
If someone could help me, would be awesome!
EDIT:
So I fumbled a bit with the queries:
SELECT
'Kleding'.'Naam',
'jos_virtuemart_products_nl_nl'.'product_name',
CONCAT_WS('/n', 'jos_virtuemart_products_nl_nl'.'product_desc', 'Kleding'.'Youtube_link')
FROM
'jos_virtuemart_products_nl_nl' as 't2'
INNER JOIN 'Kleding' as 't1'
ON 't2'.'product_name' = 't1'.'Naam';
But this query is incorrect for some strange reason. I cannot find why it is incorrect.
You can use INNER JOIN in UPDATE:
UPDATE
table2 as t2 INNER JOIN table1 as t1 ON t2.name = t1.name
SET
t2.text2 = CONCAT_WS('/n', t2.text2, t1.text1);
P.S.: Maybe by '/n' you mean '\n' ?
P.P.S: Before any major data change operation I suggest you to backup your database.
UPDv1:
Test your end resultset with SELECT:
SELECT
`t1`.`name` as `t1-name`,
`t2`.`name` as `t2-name`,
CONCAT_WS('/n', `t2`.`text2`, `t1`.`text1`) as `changes`
-- ^ here you will see what comes up
FROM
`table2` as `t2` INNER JOIN `table1` as `t1` ON `t2`.`name` = `t1`.`name`
-- WHERE possibly more conditions to meet your requirements
If you manage te get proper resultset with additional WHERE conditions, then move them to the UPDATE statement.

switching column data in mysql? [duplicate]

This question already has answers here:
Swapping column values in MySQL
(23 answers)
Closed 8 years ago.
i am wanting to try and run a query which will update my table ptb_messages and switch the data of two columns around, for instance heres my table:
id | to_user_id | from_user_id |
1 4 5
2 5 6
3 7 9
so what i want to try and do is switch the value of from_user_id over to to_user_id and vice versa.
i was going to use this before i realised that once i copied the value from one table to the other the the original data from the other column would then have been overwritten,
$sql = mysql_query("UPDATE ptb_messages SET ptb_messages.from_user_id=ptb_messages.to_user_id");
what i really need is a swap function,
im not really sure how i might do this but im imagining its something like this:
$sql = mysql_query("UPDATE ptb_messages SET to_user_id=from_user_id, from_user_id=to_user_id");
hope someone can help me please.
Well, mysql has a concept called user variable. You can take advantage of this to store the value and set it on the column for swapping,
UPDATE Table1
SET to_user_id = from_user_id,
from_user_id = #r1
WHERE #r1 := to_user_id
see here: http://www.sqlfiddle.com/#!2/8cd6a/1
how about joining the table with itself?
UPDATE Table1 a
INNER JOIN Table1 b
ON a.to_user_id = b.to_user_id AND
a.from_user_id = b.from_user_id
SET a.to_user_id = b.from_user_id,
a.from_user_id = b.to_user_id
see here: http://www.sqlfiddle.com/#!2/d6b4f/1
I'm going to throw an answer out there, although I think Skinny Pipes answer is a little more elegant.
You can create a temporary table to store the values in.
http://www.sqlfiddle.com/#!2/3631d/1
create table ptb_messages_temp (
id int,
tempid int);
insert into ptb_messages_temp (id, tempid) (select id, to_user_id from ptb_messages);
-- DO THE UPDATE
UPDATE ptb_messages
LEFT OUTER JOIN ptb_messages_temp on ptb_messages.id=ptb_messages_temp.id
SET
ptb_messages.to_user_id=ptb_messages.from_user_id,
ptb_messages.from_user_id=ptb_messages_temp.tempid;