Jump to content

Help with LIKE search and Many to One relationship... :( GURUs???


GregL83

Recommended Posts

Hello,

 

I'll create a case study for this to make sense...

 

I have two tables T1 and T2.

 

T1 has a list of books.  T2 has pictures associated with the books in T1.  More than one picture can exist for a book in T1.  So, the books have a 1 to many relationship with the pictures.

 

I created a search to return books from T1... And it works fine.  I then wanted to return a picture with each book from T2.  The problem is that T2 may have more than one image associate with a book in T1.  This results in the book being returned as a result more than once...  I have been search for logic to only return the latest (i have timestamps) picture from T2 for each book in T1 so that I don't return duplicate results of books.

 

So far my query is as follows:

Link to comment
Share on other sites

I needed to create another post cause the wiz editor was acting up...

 

query:

$query = "SELECT b.id, b.name, b.slug, p.pic FROM books b, book_pics p WHERE (name LIKE '%".$phrase."%' OR slug LIKE '%".$phrase."%') AND p.book_id = b.id"

 

Thanks in advance for any help... Your time and efforts are greatly appreciated :D

Link to comment
Share on other sites

I tried that.  I also tried to select distinct b.id ...  My thoughts are that mysql is not selecting a distinct id or name, but row.  There rows are not distinct because the picture field is different each time.  I thought your answer would have worked, but I'm still returning the same book more than once (same amount of times as images)...

Link to comment
Share on other sites

My thoughts are that mysql is not selecting a distinct id or name, but row.

 

You are correct DISTINCT doesn't work on fields it's not a FUNCTION

 

My code should work as it:

 

1. Selects all matching records from pictures with books (one book, multiple images)

2. Orders these records according to the pictures date in reverse order (latest first) Still one book, many images

3. Applies DISTINCT for each book one image

 

Post your SQL code and an image of the result

Link to comment
Share on other sites

    $query = "SELECT DISTINCT s.id, s.name, s.slug, s.overall_comments, uc.comment FROM scenes s, user_comments uc WHERE (name LIKE '%".$phrase."%' OR slug LIKE '%".$phrase."%') AND uc.scene_id = s.id ORDER BY uc.update_time DESC";
    $result = mysql_query($query);

 

here is the two arrays outputted... they are the same record, but there are 2 associated comments....

 

Array ( [0] => 1 [id] => 1 [1] => the pour house bar & grill [name] => the pour house bar & grill [2] => the-pour-house-bar-grill-boston-massachusetts [slug] => the-pour-house-bar-grill-boston-massachusetts [3] => [overall_comments] => [4] => blah! [comment] => blah! ) Array ( [0] => 1 [id] => 1 [1] => the pour house bar & grill [name] => the pour house bar & grill [2] => the-pour-house-bar-grill-boston-massachusetts [slug] => the-pour-house-bar-grill-boston-massachusetts [3] => [overall_comments] => [4] => [comment] => )

Link to comment
Share on other sites

Just an update... I've been searching long and hard to find a solution to this problem.  I fear there is a simple solution i've overlooked.  The closest i've gotten I can limit the results to 1 record per 'scene' by using GROUP BY.  I can also retrieve the max update time using MAX(update_time); however, the comment isn't retrieved from the row with the max update time...

 

$query = "SELECT s.id, s.name, s.slug, s.overall_comments, uc.comment, MAX(uc.update_time) FROM scenes s, user_comments uc WHERE (name LIKE '%".$phrase."%' OR slug LIKE '%".$phrase."%') AND uc.scene_id = s.id GROUP BY s.id";

 

Anyone???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.