SQL JOIN Query for Retrieving Pics in Multiple Categories

  • Thread starter DaveC426913
  • Start date
  • Tags
    Sql
In summary, the conversation discusses the use of tables to organize and categorize pictures. The "Pics" table contains the names of the pictures, the "Categories" table contains the names of the categories, and the "PicsCategories" table relates pictures to categories through a many-to-many relationship. The goal is to use a subquery to return all picture rows that fall into specific categories. The conversation also mentions the use of aliases, "p" and "pc", for the tables in the query.
  • #1
DaveC426913
Gold Member
22,497
6,168
I've got 3 tables:
Pics - table of my pictures
Categories - table of categories that pictures will fall into
PicsCategories - a many-to-many table relating pictures to categories. So the primary key of this table consists of a pic field and a category field

So now I want to provide a couple of categories as params and have it return all rows from the Pics table that are in those categories.

Code:
Table Pics
id Name
1 Bob
2 Mary
3 Jill
4 Jane

Table Categories
id Name
1 Male
2 Female
3 Parent

Table PicsCategories
Pic Cat
1   1
1   3
2   2
3   2
3   3
4   2
4   3
So if I supply Category1=2 OR Category2=3 (females and/or parents)
It should return Pic rows:
2
3
4
I'm looking at JOIN but JOIN is meant to extract data from two tables. I do not need ANY data from the PicsCategories table.

This is what I want:
SELECT UNIQUE(*) FROM Pics WHERE id=(SELECT Pic FROM PicsCategories WHERE Categories=2 OR Categories=3)
 
Last edited:
Technology news on Phys.org
  • #2
Ah. It's a subquery.

Code:
SELECT DISTINCT * 
FROM Pics
WHERE id
IN (
SELECT Pic
FROM tblPicsCategories
WHERE Category=2
OR Category=3
)
Thanks for all your help! Couldn't have done it without you!
 
  • #3
DaveC426913 said:
Ah. It's a subquery.

Code:
SELECT DISTINCT * 
FROM Pics
WHERE id
IN (
SELECT Pic
FROM tblPicsCategories
WHERE Category=2
OR Category=3
)
Thanks for all your help! Couldn't have done it without you!
I think this works as well:

select distinct p.* from pics p, tblPicsCategories pc where p.ID = pc.pic and (pc.Category = 2 or pc.Category = 3)
 
  • #4
DavidSnider said:
I think this works as well:

select distinct p.* from pics p, tblPicsCategories pc where p.ID = pc.pic and (pc.Category = 2 or pc.Category = 3)
What are the p and pc?

I mean, I get that p means pic and pc means piccategories but...

select distinct tblPic.* from pics tblPic, tblPicsCategories tblPicsCategories where tblPic.ID = tblPicsCategories.pic and (tblPicsCategories.Category = 2 or tblPicsCategories.Category = 3)
 
  • #5
p and pc are called aliases in this case. Sometimes written with an 'as' i.e.

...from pics as p, ...

I prefer the 'as' because I think it's clearer. There are two reasons for using them

1. It's quicker
2. You need it if the same table appears twice as a join

i.e.

select a.name, b.name
from mytrees as a, mytrees as b
where a.parent_id=b.id

Where parent_id contains the id of the row that contains its parent. You need an alias just to refer to the individual tables.
 

What is a SQL JOIN query?

A SQL JOIN query is a type of SQL query that combines data from two or more tables in a database. It allows you to retrieve data from multiple tables at once based on a common column or key.

What are the different types of SQL JOINs?

The different types of SQL JOINs are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. Each type of JOIN has a different way of combining data from multiple tables.

How do I write a SQL JOIN query?

To write a SQL JOIN query, you will need to use the JOIN keyword followed by the type of JOIN you want to use. You will also need to specify the tables you want to join and the columns or keys you want to use to join them. For example, "SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;"

What is the difference between an INNER JOIN and an OUTER JOIN?

The main difference between an INNER JOIN and an OUTER JOIN is that INNER JOIN only returns rows that have matching values in both tables, while an OUTER JOIN returns all rows from both tables, regardless of whether they have a match or not.

How do I decide which type of JOIN to use?

The type of JOIN you should use depends on the data you want to retrieve. If you only want rows that have matching values in both tables, use an INNER JOIN. If you want to include all rows from one table, even if they don't have a match in the other table, use an OUTER JOIN.

Similar threads

  • Programming and Computer Science
2
Replies
51
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
7
Views
425
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Set Theory, Logic, Probability, Statistics
2
Replies
67
Views
2K
Back
Top