I have two SQL Queries:
1. Get the the next row a row of where its status is "Active", Set it also to 'Active'
2. Remove the status of the previous row from the first sql query. Meaning, I want to remove the status of current row, move it to the next.
Code
UPDATE x AS a
INNER JOIN (
SELECT id, count(*) FROM x WHERE id=(select min(id) FROM ref_school_year where id > (SELECT ID FROM x WHERE status='START'))
) AS b on a.id = b.id
SET a.status = 'START'
WHERE a.id > 0
Second sql query
UPDATE ref_school_year as a
INNER JOIN(
SELECT * FROM ref_school_year
WHERE status='START'
ORDER BY id LIMIT 1
) as b on b.id = a.id
SET a.status=null
WHERE a.id > 0
Ex.
Before Query:
-----------------------------
| Rows | Status |
-----------------------------
| 1 | Active |
| 2 | null |
After Queries:
-----------------------------
| Rows | Status |
-----------------------------
| 1 | null |
| 2 | Active |
create table test (id int, status varchar(10));
insert test(id, status) values(1, 'Active'), (2, NULL);
update test t inner join (select id from test where status = 'Active' or exists (...)) t1 on t.id = t1.id set t.status = if(t.status = 'Active', NULL, 'Active');
The key for you is to use the IF function to update both at the same time. Hope I am right.
Related
Assume I have two tables with structures as defined.
Table1:
| question_id | question_type |
| ----------- | ------------- |
| 1 | 1 |
| 1 | 2 |
Table2:
| question_id | question_type | completed |
| ----------- | ------------- | --------- |
| 1 | 1 | 1 |
Now I want to select the record which has no entry in table2, I have done this but it does not work if the question_id is same.
Select * from table1 t1
left join table2 t2 on
(t1.question_id = t2.question_id and t1.question_type = 1) or
(t1.question_id = t2.question_id and t1.question_type = 2)
where t2.completed is null
and question_id = 1
The Problem is whenever there is a record for the same question_id with different type, no record is selected where as the result should give question_id = 1 with question_type = 2
I would use exists logic here:
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE t2.question_id = t1.question_id AND
t2.question_type = t1.question_type
);
If you wanted to use a join approach, we can try using a left anti-join here:
SELECT t1.*
FROM Table1 t1
LEFT JOIN Table2 t2
ON t2.question_id = t1.question_id AND
t2.question_type = t1.question_type
WHERE t2.question_id IS NULL;
In the following table, I want to set delete = true if the total records for same orgid exceed 500
and I want to do it according to createdate such that if records exceed 500 the old records get deleted and make total records 500 for that orgid.
here is my table
Table A
+----+-------+------------------+--------+------------+
| id | orgid | transactionvalue | delete | createdate |
+----+-------+------------------+--------+------------+
| 1 | 1 | 123 | false | 05-16-2020 |
| 2 | 1 | 412 | false | 07-16-2020 |
| 3 | 2 | 762 | false | 07-16-2020 |
+----+-------+------------------+--------+------------+
Here is the query I am trying
update A
set
delete = true
where orgid = 1
and (select count(*) as records
from (select *
from A order by createdate
) as pseudotable)) >500
use subquery and join update
UPDATE tablea
INNER JOIN
(select orgid, count(*) cnt from tablea group by orgid
) b ON tablea.orgid = b.orgid
SET
delete = 'true'
where cnt>500
You can use row_number() to find the 500th record for each org and then use that information:
update tablea a join
(select a2.org_id, a2.created_date as cutoff_created_date
from (select a2.*,
row_number() over (partition by a2.org_id order by create_date desc) as seqnum
from tablea a2
) a2
where a2.seqnum = 500
) a2
on a.org_id = a2.org_id and and
a.created_date < a2.cutoff_created_date
set delete = true;
I did not try all of the other answers, they might be correct but this is what worked for me
update A
set `delete` = true
where orgid = 1 AND id IN (
SELECT id FROM A where orgid = 1
order by createdate DESC
LIMIT 500,18446744073709551615);
i have 2 tables A and B like below
Table A
+---------+--------+
| query | status |
+---------+--------+
| number1 | solved |
+---------+--------+
table B
+----+---------+---------+
| id | query | status |
+----+---------+---------+
| 1 | number1 | started |
| 2 | number1 | working |
| 3 | number1 | solved |
+----+---------+---------+
how do i check whether the latest status of table B is equal to status of Table A
i have tried first getting latest status for table 2 but i am unable to get so
select number ,max(status),max(id)
from Table B
group by number
order by max(id) asc
Here is a simple solution relying on sorting by ID and getting only first row in a sub-query. It will only returns rows that has a match between the two tables.
SELECT a.*
FROM tableA a
JOIN (SELECT query, status
FROM tableB
ORDER BY ID desc
LIMIT 1) b ON a.query = b.query AND a.status = b.status
use join and subquery
select a.* from
tablea a join
(
select * from table_name t1
where id =( select max(id) from table_name t2 where t1.query=t2.query)
) b join on a.query=b.query and a.status=b.status
select *
from A a
inner join B b on b.query = a.query
inner join
(select max(id) id, query
from B
group by query) gq on gq.id = b.id and gq.query = b.query
where a.status = b.status
Consider I have the following table:
Id | sid | email
___________________________________________________
1 | 10 | john#yahoo.com
2 | 11 | elsa#gmail.com
3 | 10 | johnconnor#gmail.com
4 | 10 | john.smith#gmail.com
5 | 12 | ninjamutant#yahoo.com
I would like to query all rows which have same "sid" by passing known "email"
So if I pass the email as "john.smith#gmail.com", it should return rows with id number 1, 3, and 4.
Try this :
select * from yourtable a
inner join (
select sid
from yourtable
where email = "john.smith#gmail.com"
) b on b.sid = a.sid
select T2.*
from my_table T1 join my_table T2
on T1.sid = T2.sid
where T1.email = 'xxx'
I'm faced with a problem where I need to update one table based on values stored in another. However, the second table contains rows which are not relevant to the query. For example:
Table1
id | active
------------
1 | Yes
2 | Yes
3 | Yes
4 | Yes
Table2
id | type | value
--------------------
1 | date | 2011
1 | name | Glen
2 | date | 2012
2 | name | Mike
I want to read the values of type 'date' and skip name, and update table1 in the process.
I've put together the following:
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
This doesn't seem to work well at all.
Any help would be great.
id is the key which joins the tables.
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
Try this:
UPDATE table1
SET active = 'no'
WHERE a.id
IN (
SELECT b.id FROM table2 WHERE type = 'date' AND value = '2011'
)
This will work with a natural join
UPDATE table1
SET active='no'
WHERE id in
(
select id from table1 natural join table2
where
type='date'
AND value='2011'
)