l!m!t Posted August 8, 2010 Share Posted August 8, 2010 I have a question - I have a table with 3 rows (id, class, value) Is it possible to get the results of two rows without doing two separate SQL query's. For example - right now I am doing select value from tablename where class='total' and id=$id I want to be able to also get the value where class='coupon' basically I want to combine the following into 1 statement as if they were two queries.. select value from tablename where class='total' and id=$id select value from tablename where class='coupon' and id=$id is that even possible .. am I making sense? Quote Link to comment https://forums.phpfreaks.com/topic/210112-multiple-selects-from-one-query/ Share on other sites More sharing options...
RussellReal Posted August 8, 2010 Share Posted August 8, 2010 select value, class from tablename where (class = 'total' OR class = 'coupon') and id=$id ORDER BY class I took the liberty to add in 'class' to the list of values retrieved from the table, because if you are combining the two of them into the same result set you will probably want to sort them.. I sort them also in the query by class so all the totals will be together with the totals and same for the coupons.. but you will still need to put an if statement in there.. if ($row->class == 'total') { /* do for total */ } else { /* you're only selecting either total or coupon so else will obviously be coupon */ } Quote Link to comment https://forums.phpfreaks.com/topic/210112-multiple-selects-from-one-query/#findComment-1096535 Share on other sites More sharing options...
l!m!t Posted August 8, 2010 Author Share Posted August 8, 2010 Hey RussellReal, Thanks for the solution! Works exactly as needed. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/210112-multiple-selects-from-one-query/#findComment-1096539 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.