Unix Technical Forum

selection criteria spread on multiple rows.

This is a discussion on selection criteria spread on multiple rows. within the MySQL forums, part of the Database Server Software category; --> Hi, I'm having the following problem. I have a table that contains information about books people read, i.e. each ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:30 AM
abraham.pinsker@gmail.com
 
Posts: n/a
Default selection criteria spread on multiple rows.

Hi,

I'm having the following problem. I have a table that contains
information about books people read, i.e. each row has two columns,
people id and book id.
I need to do the following query: retrieve the list of all people that
read ALL the books in a given list, e.g. all the people that read book
X and book Y and book Z.
Tu further complicate the situation, an extended query also include an
exclusion crieteria, i.e. a list of books that the user should not
have read.

I really have no clue on how to approach this.
Any help will be much appricated.

Abe.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:30 AM
J.O. Aho
 
Posts: n/a
Default Re: selection criteria spread on multiple rows.

abraham.pinsker@gmail.com wrote:
> I'm having the following problem. I have a table that contains
> information about books people read, i.e. each row has two columns,
> people id and book id.
> I need to do the following query: retrieve the list of all people that
> read ALL the books in a given list, e.g. all the people that read book
> X and book Y and book Z.


This part you could do with "book IN('','','')" for the books and the "GROUP
BY user" and have a COUNT(*) and only select those with a count of 3.

SELECT COUNT(*) AS Num, User FROM table WHERE Book IN('id1','id2','id3') GROUP
BY User HAVING Num=3;


> Tu further complicate the situation, an extended query also include an
> exclusion crieteria, i.e. a list of books that the user should not
> have read.


Here is a slow query (sure someone else will have a far better one, and
require that you have a recent enough mysql server that supports subqueries)

SELECT COUNT(*) AS Num, User FROM table WHERE Book IN('id1','id2','id3') AND
User NOT IN(SELECT User FROM table WHERE Book='id4') GROUP BY User HAVING Num=3;



--

//Aho
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com