Jump to content

Recommended Posts

My table columns are as follows userid, service1, service2, service3  and the data type is int(11)

 

So, sample data is userid=34532 / service1_id=564 / service2_id=453 / service3_id=232

The idea is to search the table base on  the given userid and pull back the largest value.  Which I am able to do.

SELECT greatest(service1_id,service2_id,service3_id) FROM devvtest.user_services where userid='$userid')

 

Then, I used the returned value and query the 3 service tables to get user's info.  This is where I am stuck.  I can run a query for each service as follows:

Query for Service1

select legal_name, legal_name_fn, legal_name_ln from service1 where service1_id = (SELECT greatest(service1_id,service2_id,service3_id) FROM
devvtest.user_services where userid='$userid'

Query for Service2

select legal_name, physical_first_name, physical_last_name from service2 where service2_id = (SELECT greatest(service1_id,service2_id,service3_id) FROM
devvtest.user_services where userid='$userid'

Query for Service3

select contact_first_name, contact_last_name, legal_business_name from service3 where service3_id = (SELECT greatest(service1_id,service2_id,service3_id) FROM
devvtest.user_services where userid='$userid'

 

I am trying to combine these multiple queries into one query.  Hope this makes sense.  I looked at using if() and no luck.  Much appreciation.

 

Link to comment
https://forums.phpfreaks.com/topic/185648-query-help/
Share on other sites

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.