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 > Problem formulating a query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-02-08, 12:05
sinosoidal sinosoidal is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
Problem formulating a query

Hi,

I use MySQL and i would like to write a query that could extract the information of the following scenario.

I have a table which stores statues, there are people which are going to avaliate those statues (each one can avaliate more that one statue) so i created two other tables STATUESAVALIATIONS and AVALIATIONS

where each table has the following structure:

STATUES
ID | TITLE | ...

STATUESAVALIATIONS

ID | STATUEID | USERID | AVALIATIONID

AVALIATIONS

ID | ...

Well basically i want to obtain all the statues that weren't avaliated by a determined user.

If the user has never avaliated a statue, it must return all the record, if it has already avaliated one statue, that one will not be listed, and so on.

The statues avaliated by one user must be listed to user that havent avaliated that statue.

I have a query that does something but its not perfect yet:

SELECT * FROM STATUES WHERE STATUES.ID NOT IN (SELECT STATUEID FROM STATUESAVALIATIONS)

The problem is that this way I can't differentiate users. If i try to complement the query doing SELECT STATUEID, USERID FROM STATUESAVALIATIONS mysql gives error saying i can only select one column.

Any tips?

Thank you very much,

Best regards,

Nuno Santos
Reply With Quote
  #2 (permalink)  
Old 07-02-08, 12:21
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by sinosoidal
Well basically i want to obtain all the statues that weren't avaliated by a determined user.

SELECT * FROM STATUES WHERE STATUES.ID NOT IN (SELECT STATUEID FROM STATUESAVALIATIONS)
Change code to following (I wasn't sure how you identify users so I just used 123 as an id) :
Code:
SELECT * FROM STATUES WHERE STATUES.ID NOT IN (SELECT STATUEID FROM STATUESAVALIATIONS where USERID=123)
What does avaliate mean???
Reply With Quote
  #3 (permalink)  
Old 07-02-08, 12:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT statues.title
  FROM statues
LEFT OUTER
  JOIN statuesavaliations
    ON statuesavaliations.statueid = statues.id
   AND statuesavaliations.userid = 'r937'
 WHERE statuesavaliations.userid IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-02-08, 13:03
sinosoidal sinosoidal is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
Quote:
Originally Posted by mike_bike_kite
Change code to following (I wasn't sure how you identify users so I just used 123 as an id) :
Code:
SELECT * FROM STATUES WHERE STATUES.ID NOT IN (SELECT STATUEID FROM STATUESAVALIATIONS where USERID=123)
What does avaliate mean???
oh, thats why there was no suggestion available. Avaliated was supposed to mean, object of avaliation. How can I say that?

Thx,

Nuno
Reply With Quote
  #5 (permalink)  
Old 07-02-08, 13:05
sinosoidal sinosoidal is offline
Registered User
 
Join Date: Jul 2008
Posts: 3
Quote:
Originally Posted by r937
Code:
SELECT statues.title
  FROM statues
LEFT OUTER
  JOIN statuesavaliations
    ON statuesavaliations.statueid = statues.id
   AND statuesavaliations.userid = 'r937'
 WHERE statuesavaliations.userid IS NULL
thx for the reply.

I have only realized that mike had already written a reply before seeing your. Altought it works i don't know what it means.

Is the result of mike's suggestion the same as yours?

Thx,

Nuno
Reply With Quote
  #6 (permalink)  
Old 07-02-08, 13:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
is the result the same? yes

do they perform the same? no

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-02-08, 13:53
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by sinosoidal
Avaliated was supposed to mean, object of avaliation. How can I say that?
It's not in my dictionary therefore you can define it how you please but please make it believable.

Quote:
Originally Posted by r937
is the result the same? yes

do they perform the same? no
Rudy's respone will definitely be faster assuming you have enough data to be able to spot the difference. I just altered your code to make it work.

MBK
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