简体   繁体   中英

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.

我认为您正在寻找UNION

Why don't you use a union

Select widgetid from table1   
UNION
select widgetid from table2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM