Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is this SQL an atomic operation for MySQL ?
UPDATE
api_report a
JOIN
(
SELECT
id
FROM
api_report
WHERE
api_report.success_nums = 7878
) b
ON a.id = b.id
SET
a.success_nums = 4646;
Assume that I have 2 threads to execute the upper SQL, the data will be updated by 1 of the 2 thread? or by the both 2 thread concurrently?
I wrote a demo, and it seems the data is updated by only 1 of the 2 thread.
Use correct multiple-table syntax:
UPDATE api_report a
JOIN api_report b ON a.id = b.id
SET a.success_nums = 4646
WHERE b.success_nums = 7878
Attention! this query will update ALL matched rows including one which has success_nums = 7878. fiddle. If you does not need this then add more conditions to WHERE clause, for example, AND a.success_nums != 7878.
This SQL seems to be an atomic operation for MySQL.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
A table has fields
code
batch
I want to know what code(s) are in batch 1 but not in batch 2.
What is the best SQL statement for this?
There are multiple options as follows:
Using NOT EXISTS:
SELECT DISTINCT T.CODE FROM YOUR_TABLE T
WHERE T.BATCH = 'BATCH1'
AND NOT EXISTS (SELECT 1 FROM YOUR_tABLE TT
WHERE TT.CODE = T.CODE
AND TT.BATCH = 'BATCH2');
Using LEFT JOIN:
SELECT DISTINCT B1.CODE
FROM YOUR_tABLE B1 LEFT JOIN YOUR_tABLE B2
ON B1.CODE = B2.CODE AND B2.BATCH = 'BATCH2'
WHERE B1.BATCH = 'BATCH1' AND B2.CODE IS NULL;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
update A
set step1_response='step2',
step1_editor='step2'
where ch_idx in (select B.ch_idx
from B
where B.check_e_done_ct=B.check_e_tot_ct
AND B.check_w_tot_ct=B.check_w_done_ct);
The result of select inside the parentheses is about 4000 rows.
The table to be updated (A) is about 90000 rows.
the command above takes forever to complete.
how can i re-write this query to get result faster?
thank you!
UPDATE a
JOIN b USING (ch_idx)
SET a.step1_response='step2',
a.step1_editor='step2'
WHERE b.check_e_done_ct = b.check_e_tot_ct
AND b.check_w_tot_ct = b.check_w_done_ct;
or maybe
UPDATE a
JOIN ( SELECT ch_idx
FROM b
WHERE b.check_e_done_ct = b.check_e_tot_ct
AND b.check_w_tot_ct = b.check_w_done_ct ) bb USING (ch_idx)
SET a.step1_response='step2',
a.step1_editor='step2';
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 2 years ago.
Improve this question
I have 3 tables:
request(RequestID,Name,Mobie Number,Vehicle Number,Location)
reqresponse(RequestID,ResponseID,MechanicID,MechanicName)
mechdetails(MechanicID,MechanicName,MechanicMobile,MechanicAvalability)
Now, what I want to do is select all data from the request table and display it along with details of the mechdetails table, where MechanicAvailability is set to 'busy'.
RequestID is the foreign key in reqresponse table.
Can anyone please tell me the MySQL code to do so.
I am working in PHP.
This is where I am right now:
SELECT * FROM mechdetails WHERE MechAvailability='Busy' AND
MechanicID=(SELECT MechanicID FROM reqresponse WHERE ResponseID='$rid')
I am really new to MySQL and PHP so please help me!
The query
SELECT * FROM request r
JOIN reqresponse q ON q.RequestID = r.RequestID
JOIN mechdetails m ON m.MechanicID = q.MechanicID
WHERE MechanicAvailability = 'busy'
should do the job.
To access the resulting data, use mysqli_query and mysqli_result.
You can join the tables:
select r.*, m.*
from request r
inner join reqresponse rr on rr.requestid = r.requestid
inner join mechdetails md on md.mechanicid = rr.mechanicid
where md.mechanicavailability = 'busy'
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a query where i did a join for getting two table information, All i need to do is unique the tt_code.
My SQL
SELECT cd.`tt_code`, ri.`complain_code`, ri.`repair_time`
FROM `complain_details`cd
JOIN `repair_info` ri ON `cd`.`complain_code` =`ri`.`complain_code`
WHERE `cd`.`stat` = 'n' AND `ri`.`stat` = 'n'
AND (`cd`.`ass_to_per` = 'murad.hasan' OR `ri`.`ass_to_per` = 'murad.hasan')
My current Output
Desire output is unique tt_code rows, using SQL only. I know about DISTINCT but i am unable to achieve it.
You can use GROUP BY.
SELECT cd.`tt_code`, ri.`complain_code`, ri.`repair_time`
FROM `complain_details`cd
JOIN `repair_info` ri ON `cd`.`complain_code` =`ri`.`complain_code`
WHERE `cd`.`stat` = 'n' AND `ri`.`stat` = 'n'
AND (`cd`.`ass_to_per` = 'murad.hasan' OR `ri`.`ass_to_per` = 'murad.hasan')
GROUP BY cd.`tt_code`;
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 8 years ago.
Improve this question
This is my current code
SELECT * FROM pn_queue WHERE email = '2'
Now i want to extend is with an "AND" wich is in another table. Like this:
SELECT * FROM pn_queue WHERE email = '2' AND FROM pn_cats WHERE cat = '2'
How can i make it? I will be thankfull for any ideas or solutions.
You need to use a Join
select * from pn_queue a
join pn_cats b on a.pn_cats_id = b.id -- Change this condition to whatever should match on both table.
where b.cat = '2'
and a.email = '2'
Not sure if the join condition is actually right as you don't give enought information, but just modify the condition a.pn_cast_id = b.id if it ain't.
Edit : Just realized in your example cat and email are both equals to 2 so if it should be the match value then the correct query would simply be :
select * from pn_queue a
join pn_cats b on b.cat = a.email
For more information on SQL Join, you should check this thread What is the difference between "INNER JOIN" and "OUTER JOIN"?. It explain the difference between all possible joins with diagrams/examples etc.