PDA

View Full Version : Selecting Multiple Tables



Medyman
12-09-2007, 03:38 PM
Hi guys...

Don't know if this is possible, it should be in some way.

i'm trying to selected several rows ranging across several tables within the same DB at the same time.

I have a field in each table called "feature" and I want to select all that have a value of "1" across the DB.

How can i do this?

djr33
12-10-2007, 12:56 AM
There may be a way to create a temporary table, merge them temporarily, etc., and output at the same time, though you might find it more reasonable to just do this through php using several queries at the same time.

tech_support
12-10-2007, 06:10 AM
Try this:


<?php
$tables = array('table1','table2');
$row=array();
foreach ($tables as $table) {
$sql = "SELECT * FROM `$table` WHERE feature='1'";
$result = mysql_query($sql);
while ($r = mysql_fetch_assoc($result)) {
foreach ($r as $name=>$value) {
$row[] = $value;
}
}
}
?>

It puts all the results into the array $row.