sql - get unprocessed rows that have no association in processed table - Stack Overflow

How do I get unprocessed rows that have no association in Processed_Table with the least number of quer

How do I get unprocessed rows that have no association in Processed_Table with the least number of queries (SQLite 3)?

Nested queries will work fine.

Main_Table columns: Id

Processed_Table columns: record1_id (foreign key to id of Main_Table), record2_id (foreign key to id of Main_Table)

I want to get two records from Main_table which have not yet been processed together (based on entries in Processed_Table).

Then I process these two records and insert an entry in Processed_Table with the id of record1 as record1_id and the id of record2 as record2_id.

Then when I select the next two records from Main_Table for processing I don't want already processed records.

How do I get unprocessed rows that have no association in Processed_Table with the least number of queries (SQLite 3)?

Nested queries will work fine.

Main_Table columns: Id

Processed_Table columns: record1_id (foreign key to id of Main_Table), record2_id (foreign key to id of Main_Table)

I want to get two records from Main_table which have not yet been processed together (based on entries in Processed_Table).

Then I process these two records and insert an entry in Processed_Table with the id of record1 as record1_id and the id of record2 as record2_id.

Then when I select the next two records from Main_Table for processing I don't want already processed records.

Share Improve this question edited Nov 19, 2024 at 12:15 Pankaj asked Nov 18, 2024 at 11:00 PankajPankaj 719 bronze badges 2
  • -is it always the case that only 2 unprocessed records will be present in main table and not more than 2? – samhita Commented Nov 18, 2024 at 12:58
  • @samhita No. Main table has many records. I just need two records that are not processed together yet (based on entries in Processed Table). I need to process all combinations of all records present in Main table one by one. Example if main table has 3 records then combinations will be 1 2, 1 3, 2 1, 2 3, 3 1, 3 2. I don't want to process a record with itself, just with all other records – Pankaj Commented Nov 18, 2024 at 13:09
Add a comment  | 

1 Answer 1

Reset to default 1

I tried writing the query with the example data that you provided,let me know

I inserted one record in processed table(1,2) and main_table contains (1,2,3).

Fiddle is not responding at the moment so I could not share the fiddle link.

-- insert all 
--     into main_table(id) values(1)
--  into main_table(id) values(2)
--  into main_table(id) values(3)
-- select * from dual
-- ;

-- insert into processed values(1,2) ;

   SELECT m1.Id AS record1_id, m2.Id AS record2_id
FROM main_table m1
JOIN main_table m2 ON m1.Id != m2.Id
WHERE NOT EXISTS (
  SELECT 1
  FROM processed p
  WHERE 
    p.record1_id = m1.Id AND p.record2_id = m2.Id
   -- OR (p.record1_id = m2.Id AND p.record2_id = m1.Id)
  
)
;

Output looks like below as you can see 1,2 is not present in the output.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745623445a4636659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信