Author Topic: I need help to build a query that selects from two tables  (Read 164 times)

0 Members and 1 Guest are viewing this topic.

Offline simplyiTopic starter

  • Irregular
    • View Profile
I need help to build a query that selects from two tables
« on: February 05, 2010, 11:30:03 AM »
Hello I need a help to build a query that selects from two tables. It seems like I do not get expected results when I use JOIN..
I have to tables:

Table:
books
id int
title varchar
description text

And table
tags
book_id int
tag varchar

I need to build a query that will performs a SELECT using a KEYWORD and selects a distinct books.tilte where books.title like %keyword% AND tags.tag like %keyword%. Two tables need to be searched but the result should be DISTINCT books.title, books.id, books.description.
When I run a query using JOIN I get results from both of the tables and I results are duplicating...
Or when I run a query like this

select distinct q.title, q.id from `books` q, tags t where (q.title like "%keyword%") or ( t.tag like "%keyword%")
and there are no results that match q.title I STILL GET rows from table books selected.  :(

Thank you very much for your advice!


Offline kickstart

  • Guru
  • Addict
  • *
    • View Profile
Re: I need help to build a query that selects from two tables
« Reply #1 on: February 06, 2010, 05:39:06 AM »
Hi

Reason you last query is bringing back rows is because it is doing a CROSS JOIN, and bring back every combination of rows from the books and tags tables, then discarding those which have neither a title or tag matching your keywords.

Try something like this

Code: [Select]
SELECT distinct q.title, q.id
FROM books q
INNER JOIN tags t
ON q.id = t.book_id
WHERE q.title like '%keyword%'
OR  t.tag like '%keyword%'


All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline simplyiTopic starter

  • Irregular
    • View Profile
Re: I need help to build a query that selects from two tables
« Reply #2 on: February 08, 2010, 04:22:47 PM »
Keith, thank you very much for your response.

The query that you advised me works and selects distinct records. But it works only in those cases when t.book is in the Tags table. In my case it is possible that User posts a book and does not provide any tags for it. So there will be a new record in the Books table and no record in the Tags table. So in this situation if I search for book title nothing is going to be found even when searched book title is in Book table.

The result I need to achieve is - If keyword matches Book title then query returns a record. AND if keyword is found in the Tags table, query returns a record. But records need to be distinct. No two same book record can be returned.

Can you advise me how to fix this?

Thank you!

Offline kickstart

  • Guru
  • Addict
  • *
    • View Profile
Re: I need help to build a query that selects from two tables
« Reply #3 on: February 08, 2010, 04:29:33 PM »
Hi

If you use a LEFT OUTER JOIN rather than an INNER JOIN then I think it will give you what you want.

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline simplyiTopic starter

  • Irregular
    • View Profile
Re: I need help to build a query that selects from two tables
« Reply #4 on: February 08, 2010, 04:48:31 PM »
kickstart! Works! you are the man!

Thank you!

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.