-2

I want to get all results which these queries give with one query;

1- $query = mysql_query("select * from table where view='1'");
2- $query = mysql_query("select * from table where view='2'");
3- $query = mysql_query("select * from table where view='3'");
4- $query = mysql_query("select * from table where view='4'");
.
.
.
999999- $query = mysql_query("select * from table where view='999999'");

How can I do that easily?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Cuba Libre
  • 35
  • 4
  • 1
    use `and` and `or` in your case use `or` – Robert May 23 '16 at 18:03
  • Do not use mysql_* functions: https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189 – Matt S May 23 '16 at 18:04
  • 1
    Is it a static list or `1` to `999999`? – chris85 May 23 '16 at 18:05
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 23 '16 at 18:06
  • Possible duplicate of [PHP detmine numbers in between two numbers then query](http://stackoverflow.com/questions/3035378/php-detmine-numbers-in-between-two-numbers-then-query) – devlin carnate May 23 '16 at 18:07

3 Answers3

6

Several ways. For this extreme case (or any range) use BETWEEN:

SELECT * FROM table WHERE view BETWEEN 1 AND 999999

If not a range use IN:

SELECT * FROM table WHERE view IN (1,2,3,4,999999)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thank you, I need one more thing left to do what I want is how to get duplicate results once. I mean, if there are 3 results have "7 views", l want to get 1 result (it gives the same result 3 times, i need just 1 time). How can i do this? – Cuba Libre May 23 '16 at 19:19
  • Using `SELECT *` you can't use `DISTINCT` but you could do `SELECT * FROM table WHERE view BETWEEN 1 AND 999999 GROUP BY view` – AbraCadaver May 23 '16 at 19:23
  • Omg it works! Thank you so much! I really appreciate all your help and support :)) – Cuba Libre May 23 '16 at 19:25
3

You can use a pair of >= and <= operators:

$query = 
mysql_query("select * from table where view >= '1' AND view <= '999999'");
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

if it's serialized numbers

$query = mysql_query("select * from table where view between 1 and 999999");

if it's discrete numbers

$query = mysql_query("select * from table where view in(1, 2, 3, 10, 16, 88)");

also it's not recommend to use mysql_query it's removed from php 7 use instead pdo or mysqli with preapre to prevent SQL injection

here is example for pdo

$query = $pdo->prepare("select * from table where view between ? and ?");
$query->execute([1,99999]);
Robert
  • 2,342
  • 2
  • 24
  • 41