![](/img/trans.png)
[英]MS Access: How to automatically populate a PK that is also a FK to another tables auto increment PK?
[英]How to query 2 tables relation of which is described by another pk to pk table?
只需加入他們:
SELECT *
FROM relation r
INNER JOIN items i ON r.id_items = i.id
INNER JOIN categories c ON r.id_cat = c.id
請注意,INNER JOIN將僅選擇顯示在兩個表中的記錄。 *這里選擇所有列。 如果不想全部使用,則可以這樣選擇:i.id,i.name,c.id,c.name等。
您可以像這樣簡單地嘗試LEFT JOIN
。
SELECT
*
FROM
items AS i
LEFT JOIN relation AS r ON i.id = r.id_items
LEFT JOIN categories AS c ON r.id_cat = c.id
希望這可以幫助。
上面的查詢將為您提供所有甚至在關系表中映射的注釋項,而下面的查詢將僅為您提供在表關系中定義了關系的那些項
SELECT
*
FROM
items AS i
RIGHT JOIN relation AS r ON i.id = r.id_items
LEFT JOIN categories AS c ON r.id_cat = c.id
讓我知道您是否遇到任何錯誤。
SELECT distinct(c.name) from categories c
JOIN relation as r on c.id = r.id_cat
JOIN items as c on i.id = r.id_items
WHERE i.id in (8, x, y, z, ...etc)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.