Jump to content

selecting distinct values from 2 tables


sploit

Recommended Posts

<Sorry I had posted this in the PHP Help area earlier... :? >

 

I have two (MySQL) tables: RATES and OFFERS. Both tables have a field called ITEM.

 

I need to select all distinct ITEM values from both these tables.

 

For example:

 

RATES

-------

ITEM

1

2

3

4

5

 

OFFERS

---------

ITEM

3

5

7

8

9

 

Then the result should have:

1, 2, 3, 4, 5, 7, 8, 9

 

What query should I use?

Link to comment
https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/
Share on other sites

If you use PEAR, you can use two queries and harvest the ids in a single array:

$ITEMS = array();

$sql = "SELECT ITEM FROM <first_table> WHERE ...";

$ITEMS += $db->getCol($sql);

$sql = "SELECT ITEM FROM <second_table> WHERE ...";

$ITEMS += $db->getCol($sql);

$ITEMS = array_unique($ITEMS);

You have an array with unique IDs from both tables.

 

JP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.