0

For some reason my queries aren't working in my PHP script, I'm essentially trying to make a query that gets all players whos points are greater than 0 though I'm also trying to get some players whos points are equal to 0 but whos qualified amount is greater than 0. My example code is below.

Example

 $sql = "
            SELECT * FROM allplayers WHERE points > 0 ORDER BY points DESC
            SELECT * FROM allplayers WHERE points = 0 and qualified > 0
    ";

    $q = $pdo->query($sql);
        while ($dnn = $q->fetch()) { 
              //script
        }

Table example

player       points         qualified

Alex         90             1
Amy          0              1
Jimmy        200            0
John         0              0

The query is trying to get back everyone in the table except john whos points = 0 and who hasn't qualified (qualified = 0) Thanks for reading :)

5
  • 1
    PDO does not support Multiple queries and you would need a ; between queries even if it did Commented Mar 29, 2018 at 13:33
  • @RiggsFolly I often asked myself "why" that is. Commented Mar 29, 2018 at 13:35
  • @FunkFortyNiner Do you often while away the hours pondering these mystical detials :) :) :) Commented Mar 29, 2018 at 13:36
  • 1
    All you seem to need is an OR as the sort order will put the 0-point records last anyway... Commented Mar 29, 2018 at 13:37
  • 1
    Use one query with where points > 0 or (points = 0 and qualified > 0) Commented Mar 29, 2018 at 13:45

1 Answer 1

4

As mentioned in comments, you cannot do multiple queries, and you forgot to end the first one with a semicolon. You don't need multiple queries, however. If what you want to do is eliminate any entities that have a value of 0 for both the points and qualified columns, you just need to select any that have values > 0 for either of the columns. This should work:

EDIT: As @JitendraSoftgrid pointed out, the more appropriate answer would use your original conditions because it would account for any negative values in the points and qualified columns. Here is the combined version of your two queries:

$sql = "SELECT * FROM allplayers 
        WHERE points > 0
        OR (points = 0 and qualified > 0)
        ORDER BY points DESC";
Sign up to request clarification or add additional context in comments.

13 Comments

Probably the qualified-cirteria alone would be enough, not?
And ALSO Why should the OP try this? Good answers will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer.
The OP wants to eliminate anyone who has a value of 0 for both fields. So no union is needed, and this should do the trick..
@jeroen that's a sensible answer. it can be improved, but the most important message "the only reason to run several queries is a lack of SQL knowledge" is all right
I think where points > 0 or (points = 0 and qualified > 0) would be better as OP wants :P ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.