0

I need to check for value range multiple times, this is how i do it till now:

<?php
$preischeck = mysql_query("SELECT preis FROM outfits WHERE aktiv = 'ja' and preis <= 100 $marke $farbe");
if(mysql_num_rows($preischeck) > 0) {
     echo "0 - 100 is there";
}
?>
<?php
$preischeck = mysql_query("SELECT preis FROM outfits WHERE aktiv = 'ja' and preis > 100 and preis <= 200 $marke $farbe");
if(mysql_num_rows($preischeck) > 0) {
     echo "100 - 200 is there";
}
?>

And so on.. I guess this is too much for the server and there is a better way. Any ideas?

4
  • I am not saying your solution is ideal, but you haven't stated what is wrong with it. In other words, you are asking 'Any ideas'. Ideas for what? Commented Nov 2, 2013 at 13:52
  • Did you set up your indexes correctly? Simple queries like these should not be too hard for your server. Commented Nov 2, 2013 at 13:54
  • The code works correct. I just thought there could be a better code which does the job with only one request Commented Nov 2, 2013 at 13:56
  • You should also consider using MySQLi/PDO_MySQL as MySQL is deprecated as of PHP 5.5.x (php.net/manual/de/migration55.deprecated.php) Commented Nov 2, 2013 at 14:43

2 Answers 2

1

Why don't you just use an GROUP BY in your SQL-query:

SELECT preis FROM outfits GROUP BY preis

If you want 100-blocks

SELECT FLOOR(preis / 100) * 100 FROM outfits GROUP BY FLOOR(preis / 100)

Then if 0 is in your resultset you know that 0 - 99,99, if 100 in your resultset 100-199,99 is there etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Although it is probably not a good idea, it can also be done using variables:

SELECT 
    @range1 AS range1_count,
    @range2 AS range2_count,
    @range3 AS range3_count
FROM (
    SELECT
        IF(preis <= 10, @range1 := @range1 + 1, 0),
        IF(preis > 10 AND preis <= 100, @range2 := @range2 + 1, 0),
        IF(preis > 100, @range3 := @range3 + 1, 0)
    FROM
        (SELECT @range1 := 0) i1,
        (SELECT @range2 := 0) i2,
        (SELECT @range3 := 0) i3,
        outfits
    WHERE aktiv = 'ja'
) sub1 LIMIT 1;

Comments

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.