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;
Related
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 want to retrieve some data in a table with certain criteria.
With the following criteria:
JurPengampu = 17
NRP codes must be different from one another, if the same, then the IdJurus = 17 is taken
The explanation is in the picture.
Thank you for help,,
enter image description here
Use analytical function as follows:
select * from
(select t.*,
row_number() over (partition by kodenrp order by idjurusan) as rn
from your_table t) t
where rn = 1
Or use NOT EXISTS as follows:
select t.*
from your_table t
where not exists
(select 1 from your_Table tt
where t.kodenrp = tt.kodenrp and tt.idjurusan < t.idjhurusan)
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 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
The set of three pairs { (x1,y1),(x2,y2),(x3,y3)} are said to be transitive if y1= x2 and x3 =x1 and y3 = y2.
Write a query to output all such transitive sets.
I see this as a series of joins. You can use row_number() to generate primary keys. Your requirement translates as:
with data as (select t.*, row_number() over(order by null) rn from mytable t)
select
from data d1
inner join data d2
on d2.x = d1.y
and d2.rn <> d1.rn
inner join data d2
on d3.x = d1.x
and d3.y = d2.y
and d3.rn <> d2.rn
and d3.rn <> d1.rn
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 6 years ago.
Improve this question
i want to select two columns from database id and password
and want to use those two columns data in update query
example
select id , pass from table
second update query
update table set password2=password_get_from select_query where id=id_get_from_select_query
Try something like.
update table_one as A
inner join table_two AS B on (A.id = B.id)
set A.some_column = B.some_column,
A.another_column = B.another_column
where blah