Jump to content

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.

try this:

SELECT DISTINCT r.items from rates r, offers o where...

 

It\'s telling you it\'s ambiguous because it doesn\'t know which column you want from which table because they both have the ITEM column.

 

Look at the MYSQL documentation for \'alias\'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.