Jump to content

Multiple Selects from one query??


l!m!t

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/210112-multiple-selects-from-one-query/
Share on other sites

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  */ }

Archived

This topic is now archived and is closed to further replies.

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