sploit Posted June 25, 2003 Share Posted June 25, 2003 <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 More sharing options...
barbatruc Posted June 25, 2003 Share Posted June 25, 2003 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. Link to comment https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/#findComment-2127 Share on other sites More sharing options...
sploit Posted June 26, 2003 Author Share Posted June 26, 2003 Hmm thanks. Isnt there a MySQL query that I can just run at the console/command-line to get the desired results? Link to comment https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/#findComment-2129 Share on other sites More sharing options...
techiedude Posted June 26, 2003 Share Posted June 26, 2003 Did you try \"SELECT DISTINCT ITEM from <first_table>,<second_table> where...\" That would probably strip out the duplicates. Link to comment https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/#findComment-2140 Share on other sites More sharing options...
sploit Posted June 30, 2003 Author Share Posted June 30, 2003 welp! SQL-query : SELECT DISTINCT ITEM from rates, offers LIMIT 0, 30 MySQL said: Column: \'ITEM\' in field list is ambiguous Link to comment https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/#findComment-2174 Share on other sites More sharing options...
techiedude Posted June 30, 2003 Share Posted June 30, 2003 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\' Link to comment https://forums.phpfreaks.com/topic/636-selecting-distinct-values-from-2-tables/#findComment-2179 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.