I need to get the batch ids that are not assigned to a user. I had this select statement:
SELECT batch.id
FROM batch
WHERE (SELECT batch.id
FROM batch
JOIN user
WHERE batch.id = user.batch_id) "+ "!= batch.id
AND submitted = 0
AND batch.project_id = ?
It worked except when no batch_ids are assigned to users. And I can't just add another column to batch to, I tried but it requires way to much work. Is there a better way to do this? I don't care about optimization I just need it to work.
CREATE TABLE batch
(
id integer not null primary key autoincrement,
filepath varchar(255) not null,
project_id integer not null,
submitted boolean not null
);
CREATE TABLE user
(
id integer not null primary key autoincrement,
first_name varchar(255) not null,
last_name varchar(255) not null,
user_name varchar(255) not null,
password varchar(255) not null,
num_records integer not null,
email varchar(255) not null,
batch_id integer not null
);
SELECT b.id
FROM batch b
LEFT JOIN user u ON u.batch_id = b.id
WHERE u.id IS NULL
AND b.submitted = 0
AND b.project_id = ?
This is a classic type of query which can be written in two ways
select batch.id
from batch
left join user on user.batch_id = batch.id
where user.id is null
and batch.submitted = 0
and batch.project_id = ?
or
select batch.id
from batch
where not exists (select 1 from user
where user.batch_id = batch.id)
and batch.submitted = 0
and batch.project_id = ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.