Select object that has multiple relations

Example tables would be:

object

id name
0 obj0
1 obj1
2 obj2

attr

id description
0 attr0
1 attr1
2 attr2

link

o_id a_id
0 0
0 1
0 2
1 1

I now want to generate a SQL that returns only objects having at least a given set of attributes. I tried JOIN, but that returns either nothing or too much.

My attempt to get an object with attributes “attr0” and “attr1” (which should match only obj0 in this case):

SELECT o.name
FROM link l
INNER JOIN object o ON o.id = l.o_id
INNER JOIN attr a ON a.id = l.a_id
WHERE a.description = 'attr0'
  AND a.description = 'attr1'

This returns nothing because after the JOIN those are unique lines. With OR instead of AND, it returns both obj0 and obj1.
The result I want to see would be only obj0, because it’s the only one having attr0 and attr1.

Example (wanted) result:

o.id o.name
0 obj0

Is there a way to do this inside SQL, instead of having to filter in code later? I feel doing it via SQL and thus in the database would make my code much faster.

10

Find an object that has at least all given attributes

Your example is messed up, as you look for ‘attr1’ and ‘attr2’ and want obj1 returned which only has ‘attr1’.

However, you want to select an object, so select from the object table. You want to limit the result, so use a where clause. There is no need to join. Use EXISTS or IN to check whether the desired attributes exist for the object:

select *
from object
where id in (select o_id from link where a_id = (select id from attr where description = 'attr1'))
  and id in (select o_id from link where a_id = (select id from attr where description = 'attr2'));

The same with EXISTS:

select *
from object o
where exists (select null from link l where l.o_id = o.id and l.a_id = (select id from attr where description = 'attr1'))
  and exists (select null from link l where l.o_id = o.id and l.a_id = (select id from attr where description = 'attr2'));

2

Think you need something like this – a simple aggregation grouped by object and filtered with Having clause:

Select     o.id, o.name
From       object o
Inner join link l ON( l.o_id = o.id )
Group By   o.id,  o.name
Having     Count(Distinct l.a_id) = (Select Count(id) From attr)

Just for the record:
The same result for just the 2 of attrs (if that is the issue) could be done just changing the Having clause into one of the folloving two:

Having Count(Distinct Case When l.a_id IN(0, 1) Then l.a_id End) = (Select Count(id) From attr Where id IN(0, 1))
-- OR
Having Count(Distinct Case When l.a_id !=2 Then l.a_id End) = (Select Count(id) From attr Where id != 2)

See the fiddle here.

3

A simple approach would be to limit the select on o.id = 0

SELECT 
    o.name AS object_name,
    a.description AS attribute_description
FROM 
    object o
JOIN 
    link l ON o.id = l.o_id
JOIN 
    attr a ON l.a_id = a.id
WHERE 
    o.id = 0;

1

Your query is not going to work due to the “AND”. There is no register that satisfies both conditions. Try to change it for an “OR”

SELECT o.name
FROM link l
INNER JOIN object o ON o.id = l.o_id
INNER JOIN attr a ON a.id = l.a_id
WHERE a.description = 'attr1'
OR a.description = 'attr2'

Or better, you can use IN

SELECT o.name
FROM link l
INNER JOIN object o ON o.id = l.o_id
INNER JOIN attr a ON a.id = l.a_id
WHERE a.description IN ('attr1','attr2');

Then you can filter for the object you want:

SELECT o.name
FROM link l
INNER JOIN object o ON o.id = l.o_id
INNER JOIN attr a ON a.id = l.a_id
WHERE a.description IN ('attr1','attr2') AND o_id=0;

If you want both ATTR:

SELECT o.id, o.name
FROM object o
JOIN link l ON o.id = l.o_id
JOIN attr a ON l.a_id = a.id
WHERE a.description IN ('attr1', 'attr2')
GROUP BY o.id, o.name
HAVING COUNT(DISTINCT a.id) = 2;

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật