I have 2 tables
TBL 1: property TBL 2: property_detail
--------------------- --------------------------------------
Id Name status property_id param value
--------------------- --------------------------------------
1 X 1 1 parking two-wheeler
2 Y 1 1 furnishing furnished
3 Z 0 2 parking car-parking
4 A 1 2 furnishing semi-furnished
5 B 1 3 furnishing furnished
6 C 0 4 parking car-parking
"property_id" column in "property_detail" is foreign key of "Id" column in "property"
I want search result for status=1 and (param="parking" and value="car-parking") and (param="furnishing" and value="furnished")
From above Example table, the result will be
Result
-------------
id name
-------------
2 Y
How to achieve this?
you can get your desired result set by using join twice with details table
select p.*
from property p
join property_detail d on (p.Id = d.property_id)
join property_detail d1 on (p.Id = d1.property_id)
where p.status=1
and d.param='parking' and d.value='car-parking'
and d1.param='furnishing' and d1.value='semi-furnished';
Another way you can also use below query using having clause and sum function
select p.*
from property p
join property_detail d on (p.Id = d.property_id)
where p.status=1
group by p.Id
having sum(param="parking" and value="car-parking")
and sum(param="furnishing" and value="semi-furnished")
DEMO
SELECT property.id, property.name
FROM property INNER JOIN property_detail
ON property.id = property_detail.property_id
WHERE
(`param`="parking" AND `value`="car-parking")
AND
(`param`="furnishing" AND `value`="furnished")
AND status = 1;
can you try this one? I'm not sure though.. but it'll give you an idea.
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 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
(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;
I'm trying to get multiple attributes (e.g bedrooms, bathrooms, etc) from a few tables yes I can only seem to get one attribute with my current query. What am I missing?
What I am ultimately trying to find is property_id's with 2 bathrooms and 2 bedrooms. In this case, my desired results are property id's 1 and 4.
Here's my example on sqlfiddle
Example tables:
property
--------
id
1
2
3
4
attribute
---------
id name
1 bedrooms
2 bathrooms
attribute_value
---------------------
id attribute_id value
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
property_attributes
------------------------------
property_id attribute_value_id
1 2
1 5
2 2
2 4
3 1
3 4
4 2
4 5
This query can produce the results I want if I only want to see a query based on single attribute:
SELECT p.property_id
FROM property p
INNER JOIN property_attribute pa ON p.property_id = pa.property_id
INNER JOIN property_area pc ON p.property_id = pc.property_id
WHERE pa.attribute_value_id
IN (
SELECT av.attribute_value_id
FROM attribute_value av
INNER JOIN attribute a ON av.attribute_id = a.attribute_id
WHERE a.name like 'bedrooms' AND av.value like '2')
But what must I do to get results with more attributes (e.g. WHERE a.name like 'bathrooms' AND av.value like '2')? If I add another subquery in the main where clause, it doesn't return any results.
/// EDIT ///
here is the solution that worked for me if anyone else winds up in such a situation:
SELECT p.property_id
FROM property p
INNER JOIN property_attribute pa ON p.property_id = pa.property_id
INNER JOIN property_attribute pa2 ON p.property_id = pa2.property_id
INNER JOIN property_attribute pa3 ON p.property_id = pa3.property_id
WHERE pa.attribute_value_id = 2
AND pa2.attribute_value_id = 5
AND pa3.attribute_value_id = 7
This makes usage of a self-join.
You have an awful lot of indirection in your schema. Is there any particular point why you need the property_attribute table? Do you expect a single attribute value to be shared by many properties?
In any case, here is the query for you. Very much in the spirit of the two possible duplicates I mentioned above.
SELECT property.property_id,
bedrooms.value AS bedrooms,
bathrooms.value AS bathrooms
FROM property,
attribute AS bedrooms_attr,
attribute_value AS bedrooms,
property_attribute AS bedrooms_prop,
attribute AS bathrooms_attr,
attribute_value AS bathrooms,
property_attribute AS bathrooms_prop
WHERE bedrooms_attr.name = 'bedrooms'
AND bedrooms.attribute_id = bedrooms_attr.attribute_id
AND bedrooms.attribute_value_id = bedrooms_prop.attribute_value_id
AND bedrooms_prop.property_id = property.property_id
AND bedrooms.value = 2
AND bathrooms_attr.name = 'bathrooms'
AND bathrooms.attribute_id = bathrooms_attr.attribute_id
AND bathrooms.attribute_value_id = bathrooms_prop.attribute_value_id
AND bathrooms_prop.property_id = property.property_id
AND bathrooms.value = 2
Thanks for providing a fiddle with data to play with.
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"