If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Sql Query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-13-09, 08:35
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
Red face Sql Query help

Hi,

We have one situation. We have table something like this:

name value property_id
x y 1
x1 y1 1
x3 y3 1

Now i need to fetch the property_id and i have the values of name and value pairs

i know x=y,x1=y1,x3=y3.
One option is using self join..is there any other option for doing this?
Reply With Quote
  #2 (permalink)  
Old 08-13-09, 09:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
could you repeat the question please

i don't understand why x=y
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-13-09, 09:21
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
I mean to say

i know these values
name 'x' has value 'y'
name 'x1' has value 'y1' and so on.
I need to query for multiple conditions.
Reply With Quote
  #4 (permalink)  
Old 08-13-09, 09:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
which multiple conditions do you need to query for?

maybe it would make more sense if you talked about your actual problem instead of faking it with x's and y's
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-14-09, 05:05
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
Ok..
The condition is in this way

Table is : ex_property
It has following properties:

name ='p' , value='071'
name='hf' , value='000'
name='main', value='4'
name='second',value='5'

Now when we say

"select property_id from ex_property where name='p' and value='071'"
It returns list of ids say (100,200,800,900,120)

and when we say
"select property_id from ex_property where name='hf' and value='000'
It returns list of ids say (100,800,765)

and so for other conditions.

In result we want the intersection of all these four conditions.
I hope you get the condition.
Reply With Quote
  #6 (permalink)  
Old 08-14-09, 07:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT property_id 
  FROM ex_property 
 WHERE ( name='p' AND value='071' )
    OR ( name='hf' AND value='000' )
GROUP
    BY property_id
HAVING COUNT(*) = 2
by the way, the design you have there is called entity-attribute-value, or EAV, and it is notoriously difficult to work with

for instance, see
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-14-09, 07:25
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
Thanks :-)
yes i will read these articles..but its leagcy db so need to do designing.

But only one thing is that , i want the and condition for two conditions.
Or will return to me more results.

WHERE ( name='p' AND value='072' )
or ( name='hf' AND value='000' )

here first condition matches it will return all ids.But its not applicable.
I want to have the "and" condition of two conditions.
Reply With Quote
  #8 (permalink)  
Old 08-14-09, 07:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by SQLDBUSer123
But only one thing is that , i want the and condition for two conditions.
Or will return to me more results.
you need to test what i gave you, to understand that it ~is~ the correct solution
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-17-09, 02:35
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
I tried that..but seems to be results varying somewhat.

If i try some query like this

" select property_id from property where property_id in (
select property_id from property where (name in('main','main1') AND value in ('4','4'))
intersect
select property_id from property where (name in ('p','p1') and value in ('084','072'))
intersect
select property_id from property where (name in ('second','second1')) AND (value in('5','51')))"

Is there any better option for this query?
Do this query possess any performance issue?
Reply With Quote
  #10 (permalink)  
Old 08-17-09, 04:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by SQLDBUSer123
I tried that..but seems to be results varying somewhat.
oh

well, i've been writing SQL for over 20 years and as far as i know, the query i gave you is the solution you asked for

if you want to play around with INTERSECTs, be my guest

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-17-09, 05:40
SQLDBUSer123 SQLDBUSer123 is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
I tried it but it gives different results. Actually in that table
few other properties are there which have the same property_id.
Say for example:

property_id name value
12 p 1
12 hf 1
12 main 2
12 second 3
12 third 4
12 fourth 5

I think having count(*) = 2 does depend on this right?
I am not using all the properties which are having the property_id as 12.
I am using few properties to find out the property_id.
Does it matters over here?
Reply With Quote
  #12 (permalink)  
Old 08-17-09, 05:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by SQLDBUSer123
I think having count(*) = 2 does depend on this right?
not exactly

the COUNT(*)=2 HAVING condition makes sure that a property_id is returned by the query only if it has both of the name/value conditions in the WHERE clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On