SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with query.

    Here's my table specs
    Code:
    `clients` (
      `id` tinyint(11) NOT NULL auto_increment,
      `name` varchar(70) NOT NULL default '',
      `email` varchar(100) NOT NULL default '',
      `title` varchar(50) NOT NULL default '',
      `phone` varchar(20) NOT NULL default '',
      `company` tinyint(11) NOT NULL default '0',
      `owner` tinyint(11) NOT NULL default '0',
      `private` enum('yes','no') NOT NULL default 'no',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM
    1st query: "SELECT * FROM clients WHERE private='no'";
    2nd query: "SELECT * FROM clients WHERE private='yes' and owner='$owner'";

    Is there a way i can join those statement on the condition of 'company'. Because i would like to get all clients from 1st and 2nd query where the company field ='$company';



    Any suggestions?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select name
         , address
         , hatsize
      from clients 
     where company = '$company'
       and (
           private='no'
        or (
           private='yes' 
       and owner='$owner'
           )
           )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx r937, i was a little off.

    Code:
    SELECT * FROM clients WHERE company = '$company' AND private='no' OR (private='yes' AND owner='$owner') ORDER BY name;

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    make sure you test that thoroughly

    my interpretation of your original request involved another set of parentheses for the logic to work out correctly
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You might want to put some ( ) in there to guarentee it returns what you expect. I never rely on getting the precidence correct and always force it with brackets.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •