acctman Posted March 8, 2009 Share Posted March 8, 2009 how can i combine these two sql statements into one? basically i need to add up both tables and get a total / sum $en['num_pictures'] = mysql_num_rows(mysql_query("SELECT `i_id` FROM `rate_pictures` WHERE `i_status` != '0' AND `i_user` = '".$line[m_id]."'")); $en['num_mobpics'] = mysql_num_rows(mysql_query("SELECT `mob_id` FROM `rate_mobile_pics` WHERE `mob_confirmed` != '0' AND `mob_user` = '".$line[m_id]."'")); Quote Link to comment https://forums.phpfreaks.com/topic/148425-solved-combine-two-sql-statements/ Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 Looks like you need a JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/148425-solved-combine-two-sql-statements/#findComment-779332 Share on other sites More sharing options...
kickstart Posted March 8, 2009 Share Posted March 8, 2009 Hi Not sure exactly how you want to combine them, as keeping them seperate looks to be a pretty simple way of just getting the 2 counts. Unless you want a count of the total of both of them. In which case you could union the 2 pieces of sql together $en['num_pictures'] = mysql_num_rows(mysql_query("SELECT `i_id` FROM `rate_pictures` WHERE `i_status` != '0' AND `i_user` = '".$line[m_id]."' UNION ALL SELECT `mob_id` FROM `rate_mobile_pics` WHERE `mob_confirmed` != '0' AND `mob_user` = '".$line[m_id]."'")); I have used UNION ALL as UNION will remove duplicates, and I would guess that you have some i_id values which are the same as some mod_id values. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148425-solved-combine-two-sql-statements/#findComment-779474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.