Go3Team
Go3Team

Reputation: 145

Multiple WHERE AND Querys - Mysql / PHP

Trying to get the results of one query into the WHERE statement of another:

$query1 = "SELECT DISTINCT company_id FROM location WHERE state = 'XX'";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
    //Collect WHERE STATEMENT HERE
}

I've tried the following:

$q1 = "SELECT company FROM company WHERE id = '16', id = '7' ORDER BY company";
$q1 = "SELECT company FROM company WHERE id = '16' AND id = '7' ORDER BY company";
$q1 = "SELECT company FROM company WHERE id = '16' && id = '7' ORDER BY company";

Along with other variations

Googling has only provided multiple WHERE AND if using different table names, but not the same. Anyone have any pointers?

Upvotes: 1

Views: 98

Answers (3)

Nishant Jani
Nishant Jani

Reputation: 1993

for a generalized code :

$ids = array (
[0] => 2
[1] => 4
[2] => 19
[3] => 16
);  

$ids = join(',',$ids);  
$sql = "SELECT * FROM company WHERE id IN ($ids)";

reffer : same example

Upvotes: 1

Mihai Stancu
Mihai Stancu

Reputation: 16127

You could also use IN:

$x = array('16', '7');

$q1 = "SELECT company FROM company WHERE id IN(". implode(',', $x) .") ORDER BY company";

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39724

use OR

$q1 = "SELECT company FROM company WHERE (id = '16' OR id = '7') ORDER BY company";

Upvotes: 0

Related Questions