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 > [search] Mysql query with relational tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-22-05, 10:32
djavet djavet is offline
Registered User
 
Join Date: Sep 2005
Posts: 5
[search] Mysql query with relational tables

Hello

I've a little Mysql query problem.
I've 2 tables for a address book application.

Code:
CREATE TABLE `contacts` (
  `pid` int(11) NOT NULL auto_increment,
  `organisation` varchar(50) NOT NULL default '',
  `title` varchar(50) default NULL,
  `nom` varchar(50) default NULL,
  `prenom` varchar(50) default NULL,
  `address1` varchar(150) default NULL,
  `address2` varchar(150) default NULL,
  `npa` varchar(10) default NULL,
  `city` varchar(50) default NULL,
  `code_pays_rel` varchar(50) NOT NULL default '',
  `phone` varchar(25) default NULL,
  `fax` varchar(25) default NULL,
  `mobile` varchar(25) default NULL,
  `email` varchar(70) default NULL,
  `web_url` varchar(70) default NULL,
  `web_text` varchar(70) default NULL,
  `comment` blob,
  PRIMARY KEY  (`pid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1126 ;
And a country table with 236 entries:
Code:
CREATE TABLE `pays` (
  `idpays` int(11) NOT NULL auto_increment,
  `code_pays` varchar(50) default NULL,
  `pays` varchar(50) default NULL,
  PRIMARY KEY  (`idpays`),
  UNIQUE KEY `code_pays` (`code_pays`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=237 ;
When I search with a keyword only in contact. No problem (simple!):
Code:
SELECT *
FROM contacts
WHERE nom like '%SearchString%' or 
web_url like '%SearchString%' or 
organisation like '%SearchString%'
ORDER BY organisation, nom ASC

Result: 1 (and this is correct)
but if I add the "pays" table to add the name of the country for my contact name regarding the "code_pays_rel" field in contact table. I've 236 entries instead of 1 ;o(
Code:
SELECT *
FROM contacts, pays
WHERE nom like '%SearchString%' or 
web_url like '%SearchString%' or 
organisation like '%SearchString%' or 
pays like '%SearchString%' AND 
contacts.code_pays_rel=pays.code_pays
ORDER BY pays, organisation, nom ASC

Result: 236 !!!
What I am doing wrong?

A lot of thx for your help and time.

Regards,
Dominique Javet
Reply With Quote
  #2 (permalink)  
Old 09-22-05, 10:34
tombell tombell is offline
Registered User
 
Join Date: Jun 2005
Posts: 23
are there any columns relating the two tables?
Reply With Quote
  #3 (permalink)  
Old 09-22-05, 10:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by djavet
What I am doing wrong?
you are mixing ANDs and ORs without the proper parentheses

try this --
Code:
  FROM contacts
     , pays
 WHERE (
       nom like '%SearchString%' 
    or web_url like '%SearchString%' 
    or organisation like '%SearchString%' 
    or pays like '%SearchString%' 
       )
   AND contacts.code_pays_rel=pays.code_pays
alternatively, and this is the recommended approach, use this syntax and the problem does not appear:
Code:
  FROM contacts
inner
  join pays
    on contacts.code_pays_rel=pays.code_pays
 WHERE nom like '%SearchString%' 
    or web_url like '%SearchString%' 
    or organisation like '%SearchString%' 
    or pays like '%SearchString%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-22-05, 10:36
djavet djavet is offline
Registered User
 
Join Date: Sep 2005
Posts: 5
Yope:
Code:
contacts.code_pays_rel=pays.code_pays
Dom
Reply With Quote
  #5 (permalink)  
Old 09-22-05, 10:39
tombell tombell is offline
Registered User
 
Join Date: Jun 2005
Posts: 23
Sorry about that.
Didn't notice it.
Anyway, Rudy answered your question
Reply With Quote
  #6 (permalink)  
Old 09-22-05, 10:40
djavet djavet is offline
Registered User
 
Join Date: Sep 2005
Posts: 5
Excellent! It's working well with the inner join.
Every day I learn!

Thx a lot. Dominique
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