Working on Android SMS notification using sqlite.
Got three tables named
people: `id`, `people_name`
info: `id`,`info_name`
people_info: `auto-increment id`,
`people_id` <- foreign key (people.id)
`info_id` <- foreign key (info.id)
When people are notified of the info the people_info
adds the two ids. How to make the WHERE clause to check people is informed; how to filter people who are fully notified and prevent notification redundancy.
To get rows from people
where there isn't a row related to that people
and a particular row in info
, you can use an anti-join pattern.
For example:
SELECT p.id AS `person_id`
, p.people_name AS `person_name`
FROM people p
LEFT
JOIN people_info i
ON i.people_id = p.id
AND i.info_id = ?
WHERE i.people_id IS NULL
ORDER BY p.id
This isn't the only query pattern that returns this result. There are a couple of others, for example, using a NOT EXISTS and correlated subquery:
SELECT p.id AS `person_id`
, p.people_name AS `person_name`
FROM people p
WHERE NOT EXISTS ( SELECT 1
FROM people_info i
WHERE i.people_id = p.id
AND i.info_id = ?
)
ORDER BY p.id
If you want to return all (people,info)
tuples that aren't related, rather than a just a single info
, that can be achieved as well. The specification provided is ambiguous about what value(s) are going to be provided, and an example of the expected output. The answer above addresses only one of several possible interpretations.
If we want to involve all three tables... to get all info for all people who have not yet been notified, we can use an anti-join pattern:
SELECT p.id AS `person_id`
, p.people_name AS `person_name`
, i.id AS `info_id`
, i.info_name AS `info_name`
FROM people p
CROSS
JOIN info i
LEFT
JOIN people_info n
ON n.people_id = p.id
AND n.info_id = i.id
WHERE n.people_id IS NULL
ORDER BY p.id, i.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.