简体   繁体   中英

One-To-Many Relationship Query

I am really stuck on how to create the appropriate select statement and would appreciate any guidance you can offer.

I have created a mini document management system that allows users to upload files and to categorize those files by categories. Each file can have one or many categories selected. Following are my tables:

Table: files (pKey (primary key), file_name, file_description, file_path)

pKey  file_name  file_description        file_path
1     IT001.DOC  Network Design Document /common/it/
2     IT002.DOC  Desktop Standards       /common/it/
3     IT003.DOC  Laptop Standards        /common/it/

There are other departments in addition from IT so the path field also changes (Just thought I would toss that bit of datum in)

Table: categories (pKey (primary key), category_description)

pKey  category_description
1     Central Missouri Campus
2     Eastern Missouri Campus
3     Western Missouri Campus
4     Desktops
5     Laptops
6     Networks
7     Printers

Of course there are other categories as well, this is just a sampling

Table: category_xref (pKey (primary key), fk_file_id, fk_category_id)

pKey  fk_file_id  fk_category_id
1     1           1
2     1           2
3     1           6
4     2           2
5     2           3
6     2           4
7     3           1
8     3           2
9     3           3
10    3           5

When the user searches for related documents they are presented a form with the Category checkboxes. By choosing Central, they get all files that have been marked as Central. By choosing Desktops, they get any documents that have been marked as Desktops. However, when they select Central AND Desktops they get any document that is either Central OR Desktops. I need to figure out how to get only those documents that are BOTH Central AND Desktops AND any other checkboxes they have selected, and exclude any that do not contain ALL the checkboxes selected.

SELECT f.pkID, f.file_name, f.file_description, f.file_path, cox.fk_category_id 
FROM files f
JOIN category_xref  cox ON cox.fk_file_id = f.pkID
WHERE cox.fk_category_id IN (59, 69)
ORDER BY f.file_name ASC, cox.fk_category_id ASC

The simplest way would probably be:

SELECT f.pkID, f.file_name, f.file_description, f.file_path
FROM files f
JOIN category_xref  cox ON cox.fk_file_id = f.pkID
WHERE cox.fk_category_id IN (59, 69)
GROUP BY f.pkID
HAVING count(distinct cox.fk_category_id)=2
ORDER BY f.file_name ASC

You can try something in these lines:

SELECT f.pkID, f.file_name, f.file_description, f.file_path
FROM files f
RIGHT JOIN category_xref  cox1 ON cox.fk_file_id = f.pkID AND cox1.fk_category_id = 59 
RIGHT JOIN category_xref  cox2 ON cox.fk_file_id = f.pkID AND cox2.fk_category_id = 69 
ORDER BY f.file_name ASC

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM