Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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