SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mySQL query help

    I have the following mySQL query to run on my database using PHP It basically pulls out all article titles with a certain category and subcategory from the 'articles' table to display on the page.

    PHP Code:
    SELECT FROM articles WHERE category 'cars' AND subcat 'events' ORDER BY id DESC 
    But I want to be able to select a row that meets the following criteria:

    Category = cars
    Subcategory = events OR tech

    I have tried this:

    PHP Code:
    SELECT FROM articles WHERE category 'cars' AND subcat 'events' OR subcat 'tech' ORDER BY id DESC 
    But this also selects other rows with other categories.

    Can anyone point me in the right direction here?

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You need to add ( ) brackets:

    PHP Code:
    SELECT FROM articles WHERE category 'cars' AND  ( subcat 'events' OR subcat 'tech' ORDER BY id DESC 
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Originally posted by pippo
    Hi,

    You need to add ( ) brackets:

    PHP Code:
    SELECT FROM articles WHERE category 'cars' AND  ( subcat 'events' OR subcat 'tech' ORDER BY id DESC 
    Or you can use the SQL IN function!

    PHP Code:
    SELECT FROM articles WHERE category 'cars' AND subcat IN('events''tech'ORDER BY id DESC 
    Hopefully that works!

    --Vinnie

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys.

    I used the first solution in the end, works fine

    -Sam


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
  •