I have many to many relation
table: images
id imageName
1 pic01
2 pic02
3 pic03
table: imagesKeywords
imageId keywordId
1 2
1 3
1 4
2 3
3 1
3 4
3 2
table: keywords
id keywordName
1 car
2 tree
3 cat
4 phone
Each image has some keywords, and different images can have the same keyword.
I need to make a search for images , they have a specific keywordName's.
example-1: search for car and phone
the result should be : pic03
example-2: search for tree and phone
the result should be : pic01, pic03
You appear to want JOIN with GROUP BY Clause :
select i.imageName
from images i inner join
imagesKeywords ik
on ik.imageId = i.id inner join
keywords k
on k.id = ik.keywordId
where k.keywordName in ('car', 'phone')
group by i.imageName
having count(*) = 2;
As I understand you, this should work:
select i.imageName as name from keywords k
join imagesKeywords iK on k.id = iK.keywordId
join images i on iK.imageId = i.id
group by i.imageName;
One possible solution,
with subquery as
(select i1.imageName, k1.keywordName from keywords k1 join imagekeywords ik1 on k1.id=ik1.keywordId join images i1 on i1.id = ik1.imageId )
select a.imageName from subquery a join subquery b on a.imageName=b.imageName where a.keywordName ='car' and b.keywordName='phone';
Related
I need help with a mysql query using the following two tables:
profiles (TABLE 1)
id user_id gender age height bodytype
1 1 1 57 1 2
2 2 2 32 2 1
profile_lookup (TABLE 2)
id option_group option_value option_name
1 gender 1 Female
2 gender 2 Male
3 gender 3 Prefer not to say
4 height 1 5 ft - 6 in
5 height 2 5ft - 9 in
6 bodytype 1 Petite/slim
7 bodytype 2 Average
There are whole lot of other options and option values that i am omitting for the sake of brevity
I am interested to do inner join queries using the syntax as shown below:
SELECT *
FROM profiles
WHERE bodytype = 2
JOIN profile_lookup
ON profiles.gender = profile_lookup..... (not sure)
Request help with using the correct syntax using the above two tables. Thanks
I think you want:
SELECT p.*, plg.option_name as gender
FROM profiles p INNER JOIN
profile_lookup plg
ON plg.option_group = 'gender' and
plg.option_value = p.gender
WHERE p.bodytype = 2 ;
You can extend this to other columns. You might want a LEFT JOIN in case some values don't match (i.e. are NULL):
SELECT p.*, plg.option_name as gender, plh.option_name as height
FROM profiles p LEFT JOIN
profile_lookup plg
ON plg.option_group = 'gender' AND
plg.option_value = p.gender LEFT JOIN
profile_lookup plh
ON plh.option_group = 'height' AND
plh.option_value = p.height
WHERE p.bodytype = 2
The where clause should be after the JOIN clause
SELECT * FROM profiles
INNER JOIN profile_lookup ON profiles.gender = profile_lookup.option_value
and profile_lookup.option_group = 'gender'
WHERE profiles.bodytype = 2
and for the join you need the proper profile_lookup.option_value
So i have a database with some tables. Now i want a query that gets data from 3 tables. First lets see what the databases are
omschrijvingVoorraad
-ID 1
-userID 1
-omschrijvingID 6
-min 4
omschrijving
-ID 6
-omschrijving Cola (blikje 330ml)
voorraad
-ID 20
-userID 1
-omschrijvingID 6
-aantal 2
Now i want to make a query that will show the next line:
Cola (blikje 330ml) Aantal 2 minmaal 4
I searched around and came up with below but it is not working. It doesn't give an error but just an empty result
$queryOm="SELECT omschrijvingVoorraad.ID, omschrijvingID, omschrijving, vAantal, min
FROM omschrijvingVoorraad
LEFT JOIN omschrijving ON omschrijving.ID = omschrijvingVoorraad.omschrijvingID
INNER JOIN ( SELECT omschrijvingID vid, SUM( aantal ) vAantal
FROM voorraad WHERE userID='$userID' ) p ON vid = omschrijvingVoorraad.omschrijvingID
WHERE userID='$userID'
LIMIT $offset, $perPage";
Where offcourse the $offset and $perPage are being defined earlier in the code.
So can anyone tell me where I went wrong? What should I change to get the correct result?
Looking to your schema and your expected result seem you need this query
select a.ID, a.omschrijvingID, b.omschrijving, sum(c.aantal), a.min
from omschrijvingVoorraad as a
inner join omschrijving as b on a.omschrijvingID = b.ID
inner join voorraad as c on a.omschrijvingID = c.omschrijvingID
group by a.ID, a.omschrijvingID, b.omschrijving, a.min
I have 3 Tables (with prefix "pindex_")
names photos link
id, name id, filename photo_id, name_id
-------- ------------ -----------------
1 , leo 1 , aa.jpg 1 , 1
2 , liz 2 , bb.jpg
3 , ann
So Leo is connected to Photo aa.jpg
Now I would like to get all names that are not connected to aa.jpg.
Result should be: Liz (2), Ann (3).
I have tried with this but it isn't working so far:
select nm.name from pindex_names nm
join pindex_link pl on pl.name_id = nm.id
join pindex_photos pp on pl.photo_id = pp.id
where pl.name_id !='$id'
And my second question is how i can SUM up a Col within a joined table?
For exaple I would like to Count how many names are connected to a certain photo.
Here is one method:
select n.*
from names n
where not exists (select 1
from link l join
photos p
on l.photo_id = p.id
where l.name_id = n.id and
p.filename = 'aa.jpg'
);
SELECT *
FROM pindex_names
WHERE id NOT IN
(
SELECT name_id
FROM pindex_link pl
INNER JOIN pindex_photos pp
ON pl.photo_id = pp.id
WHERE filename='aa.jpg'
)
So I basicly have a database that looks like this:
card_types
id description price
1 Card1 1.00
2 Card2 2.00
3 Card3 3.00
card_occasions
id occasion
1 birthday
2 graduation
3 thank you
4 other
type_occasions
ID TypeID OccasionID
1 1 1
2 1 2
3 1 4
4 2 3
I am trying to do an inner join using WHERE card_type.ID = 1 resulting in a similar output but I have no idea where to begin.
Example output:
card_occasion.ID card_occasions.Name
1 birthday
2 graduation
4 other
Any help would be appreciated.
Thanks.
Since type_occasions already owns the typeid, you don't need to join the type table.
SELECT o.id, o.occassion
FROM card_occasions o
INNER JOIN type_occasions t ON t.occassionid = o.id
WHERE t.typeid = 1
You begin with the table where you want values. After Join the table you need to make the relation.
SELECT card_occasions.id, card_occasions.occasion
FROM card_occasion co
INNER JOIN type_occasions to ON (to.OccasionID = co.id)
^ the relation between two table
WHERE ct.ID = 1
SELECT A.id,A.occasion FROM card_occasions A JOIN type_occasions B ON B.OccasionID= A.id AND B.TypeID=1
And if you really want to link all three tables for reason we don't see here you may use this method. This starts linking from the type_occasion table to appropriate "basic data" tables.
Select
typeo.OccasionID, co.occasion as Name
From type_occasion typeo
JOIN card_type ct ON (typeo.TypeID=ct.id)
JOIN card_occasion co ON (typeo.OccasionID=co.id)
Where
typeo.TypeID=1
-- ct.id=1
I have the 3 Following Tables:
objects:
ObjectID ObjectDescription
1 "first"
2 "second"
attributes:
AttributeID AttributeDescription
1 "att1"
2 "att2"
3 "att3"
4 "att4"
attributelink:
AttributeID ObjectID
1 1
2 1
4 1
Now my question: I want now have some attributes selected and want to know, which Object has all my selected Attributes. I've tried the following:
SELECT * FROM `objects`
INNER JOIN `attributelink`
ON `objects`.`ObjectID` = `attributelink`.`ObjectID`
WHERE `attributelink`.`AttributeID` =1 AND `attributelink`.`AttributeID` =2
GROUP BY `objects`.`ObjectID`
That obviously doesn't work, because one row can't have 2 AttributeIDs, but how can I archieve this?
You have to join on the attributelink table once for each selected attribute you want to check for:
SELECT o.ObjectID
FROM objects o
INNER JOIN attributelink a1 ON o.ObjectID = a1.ObjectID AND a1.AttributeID = 1
INNER JOIN attributelink a2 ON o.ObjectID = a2.ObjectID AND a2.AttributeID = 2
GROUP BY o.ObjectID
Your test data doesn't show a whole lot about whether it works or not, but FWIW, here's the sqlfiddle.
Another way to do it is to use COUNT DISTINCT with HAVING and GROUP BY (sqlfiddle):
SELECT o.ObjectID
FROM objects o
INNER JOIN attributelink a ON o.ObjectID = a.ObjectID
WHERE a.AttributeID IN (1,2) --here you filter the rows on the attributes to test
GROUP BY o.ObjectID
HAVING COUNT(DISTINCT(a.AttributeID)) = 2 --# of attributes, means "having ALL"