Reputation: 73
I've a question about a SQL query.. I'm building a prototype webshop in ASP.NET Visual Studio. Now I'm looking for a solution to view my products. I've build a database in MS Access, it consists of multiple tables.
The tables which are important for my question are:
Below you'll see the relations between the tables
For me it is important to get three datatypes: Product title, price and image.
The product title, and the price are in the Product
table. The images are in the Foto
table.
Because a product can have more than one picture, there is a N - M relation between them. So I've to split it up, I did it in the Productfoto
table.
So the connection between them is:
product.artikelnummer -> productfoto.artikelnummer
productfoto.foto_id -> foto.foto_id
Then I can read the filename (in the database: foto.bestandnaam
)
I've created the first inner join, and tested it in Access, this works:
SELECT titel, prijs, foto_id
FROM Product
INNER JOIN Productfoto
ON product.artikelnummer = productfoto.artikelnummer
But I need another INNER JOIN
, how could I create that? I guess something like this (this one will give me an error)
SELECT titel, prijs, bestandnaam
FROM Product
(( INNER JOIN Productfoto ON product.artikelnummer = productfoto.artikkelnummer )
INNER JOIN foto ON productfoto.foto_id = foto.foto_id)
Can anyone help me?
Upvotes: 1
Views: 7780
Reputation: 823
One thing about the use of linking tables
The ProductFoto table allows for N-M relations between Product and Foto indeed. Is this what you really want/need? In other words, can one Foto belong to more than one Product? If not, put the Product_Id on the Foto table. If so,...
...let's discuss JOIN.
Say we have two tables, A and B. doing a
SELECT * FROM A, B
will give you all permutations of A's rows with B's rows. We could limit the resultset by adding a WHERE
clause, like WHERE A.a='lekker hoor!'
, or a way cooler WHERE A.id=B.a_id
. Which actually starts to look like a JOIN
result!
Lets do a proper JOIN
then:
SELECT * FROM A JOIN B ON A.id=B.a_id
JOIN
s actually come in LEFT OUTER
, RIGHT OUTER
and FULL INNER
or FULL OUTER
joins.
LEFT JOIN
(use of OUTER
is optional) will contain all records in the left (first) table, even if there is no corresponding records(s) in the right (second) table. RIGHT JOIN
obviously works the same way, but mirrored.FULL OUTER JOIN
both tables are optional (not quite the same as SELECT * FROM A, B
though!). FULL INNER
needs matching records from both tables (this is the default). When you do want to do more than one JOIN
, say
SELECT * FROM
A
JOIN B ON A.id=B.a_id
JOIN C ON B.id=C.b_id
You can think of the extra JOIN
as joining on an intermediate table, formed by joining A and B, especially when mixing some LEFT
/RIGHT
/INNER
/OUTER
JOIN
s.
As to your question Use something along the lines of
SELECT TOP (1) titel, prijs, bestandnaam
FROM
( -- this bracket is MS Access specific (and awkward)
Product
INNER JOIN Productfoto ON product.artikelnummer = productfoto.artikelnummer
) -- this one too
INNER JOIN foto ON productfoto.foto_id = foto.foto_id
Veel success, en doe jezelf een plezier en switch to English!
Upvotes: 1
Reputation: 6638
something like this should work:
SELECT Product.titel, Product.prijs, Foto.bestandnaam FROM Product INNER JOIN
(Foto INNER JOIN Productfoto ON Foto.[foto_id] = Productfoto.[foto_id]) ON
Product.[artikelnummer] = Productfoto.[artikelnummer];
Upvotes: 3