Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Location
    Orlando, FL
    Posts
    158
    Thanks
    4
    Thanked 8 Times in 8 Posts

    Question Best method of filtering MySQL results

    So, I have a "filter results" section of my website. In my database, I have a table of video games with a column named gameConsole that stores the name of the console the game runs on. In my "filter results" section, there is a part where you can check some checkboxes to filter the results to specific consoles. Now, I have looked everywhere for a way to query same column for different items. For example, if somebody checked PS3 and PS4, the query should pull all PS3 and PS4 games. Other than having to run multiple queries, is there a way to query for both items in the table?

    I hope this makes sense.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    You would use either OR logic or the IN() function - MySQL :: MySQL 5.0 Reference Manual :: 12.3.2 Comparison Functions and Operators
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    wojo1086 (05-20-2014)

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Location
    Orlando, FL
    Posts
    158
    Thanks
    4
    Thanked 8 Times in 8 Posts
    That wouldn't work because the IN() function works like the OR and I need it to work like an AND. I need to it to pull all PS3 games AND PS4 games from the gameConsole column, not all PS3 games OR all PS4 games.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    No, you need OR, if you used AND then your results will be 0. Write it out in a sentence and then you'll see.

    If the game is for PS3 AND PS4 -> results will be 0
    If the game is for PS3 OR PS4 -> It will get any game for PS3 OR PS4, meaning if a game is set to PS3 it will be selected, and if it's set to PS4 it will be selected. Using OR will cause it to get either one, but using AND means it has to be both.


    Your setup is up to you, however I'd recommend doing a comma separated list and using find_in_set to get them along with OR statements. This way you could have multi-platform games in the list rather than having a game entry per system or making columns for each system and having to modify the database structure each time there's a new console out.

    Ex:
    PHP Code:
    $title 'Call of Duty: Ghosts';
    $platforms 'xbox360,ps3,xboxone,ps4,pc'//Or however you name the consoles 
    Last edited by Dubz; 05-20-2014 at 05:29 AM.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • Users who have thanked Dubz for this post:

    wojo1086 (05-20-2014)

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Location
    Orlando, FL
    Posts
    158
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Wow. I completely misunderstood that AND thing. For some reason, I was thinking AND would get me PS3 and PS4, not rows where PS3 and PS4 exist together. I would totally use OR. You were both right; I apologize for that. You guys rock!


  •  

    Posting Permissions

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