182x Posted July 27, 2007 Share Posted July 27, 2007 Hey guys, I am trying to create a query in order to dynamicly popular a listbox however I am trying to fill the listbox with years such as 2007 etc based on if they are stored in the data base. The problem is that two tables have to be checked that have no relation to eachother but this caues the problem of duplication so if both tables have 2007 it will appear in the list more than once. I was just wondering if there is a way round this? Thanks. $years= "SELECT * FROM table1; $qyears = mysql_query($years, $link_id) or die(mysql_error()); $years2= "SELECT * FROM table2; $qyears2 = mysql_query($years2, $link_id) or die(mysql_error()); if (mysql_num_rows($qyears2) > 0 || mysql_num_rows($qyears1) > 0) { ?> <select name="years"> <?php //don't know what do do here while($row = mysql_fetch_assoc($qyears)) { ?> <option value="<?php echo $row['year']; ?>"><?php echo $row['year']; ?></option> <?php } } ?> </select> Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/ Share on other sites More sharing options...
nicelad_uk Posted July 27, 2007 Share Posted July 27, 2007 Just give this a try for me, dont know if it will work on MYSQL: $years= "SELECT DISTINCT(year) FROM table1,table2"; $qyears = mysql_query($years) or die(mysql_error()); ?> <select name="years"> <?php //don't know what do do here while($row = mysql_fetch_assoc($qyears)) { ?> <option value="<?php echo $row[$qyears]; ?>"><?php echo $row[$qyears]; ?></option> <?php } } ?> </select> [/quote] Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308697 Share on other sites More sharing options...
182x Posted July 27, 2007 Author Share Posted July 27, 2007 I get this error: Column 'year' in field list is ambiguous Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308700 Share on other sites More sharing options...
182x Posted July 27, 2007 Author Share Posted July 27, 2007 Still can't figure this one out, anyone got anymore ideas? Thanks. Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308780 Share on other sites More sharing options...
dg Posted July 27, 2007 Share Posted July 27, 2007 it must be present in both the tables, give from which table u want.... alias Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308784 Share on other sites More sharing options...
dg Posted July 27, 2007 Share Posted July 27, 2007 $years= "(SELECT year FROM table1 t1) UNOIN ALL (SELECT year FROM table2 t1)" Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308788 Share on other sites More sharing options...
182x Posted July 27, 2007 Author Share Posted July 27, 2007 Thanks, if its done that way is there a method that can limit the result for example if 2007 appears 8 times can it be limited to just appearing once? Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308789 Share on other sites More sharing options...
dg Posted July 27, 2007 Share Posted July 27, 2007 instead of UNION ALL use UNION Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308793 Share on other sites More sharing options...
182x Posted July 27, 2007 Author Share Posted July 27, 2007 does that still only produce one 2007 value if there are three 2007 values in table1 and four 2007 values in table2? Link to comment https://forums.phpfreaks.com/topic/62004-solved-php-query-question/#findComment-308796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.