run two queries on two tables to get one list of results with mysql/php
I have two tables each with a field we will call widgetid.
I need to run a query on both tables, that will return a single list of widgetid's from both tables.
I have no idea how to do this.
What i have now is:
$result = mysql_query("SELECT * FROM `inventory` WHERE find_in_set('$serial', items)") or die(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$widgetid = $row['widgetid'];
//Do Stuff For Each WidgetID开发者_开发百科
}
Now i need to take that same $serial, and search the second table for its list of widgetid's. But i need to still be able to "do stuff" in the same place, with both lists of widgetids as one
SELECT widgetid FROM TABLE1 WHERE find_in_set('$serial', items)
UNION
SELECT widgetid FROM TABLE2 WHERE find_in_set('$serial', items)
Is that what you are looking for? This will combine two select querys and give you it as a single result.
I think you're looking for a UNION.
Why don't you use a union
Select widgetid from table1
UNION
select widgetid from table2
精彩评论