Jump to content

[SOLVED] Is a subquery the only way?


Philip

Recommended Posts

Okay guys, I can do it this query using subqueries - but I'm wondering if there is a more efficient way to do this:

 

Settings table:

VariableValue

Setting11

Setting2999

 

Members Table:

IDNameCode1Code2

1Phil751000

2Bob065

3Joe53210

 

Let's say I want to:

Select members.id, members.name from members, settings where members.code1 > settings.setting1 and members.code2 < settings.setting

 

Right now, above query is obviously invalid, since the field name is "Variable" and "Value" (and not "Setting1"/"Setting2"), so following works:

SELECT `id`, `name`
FROM `members`
WHERE code1 > (SELECT `value` FROM `settings` WHERE `variable`='Setting1')
AND code2 < (SELECT `value` FROM `settings` WHERE `variable`='Setting2')

 

Which would return ID 3 - Joe.

 

Is there any way to simplify or make the above query more efficient.

 

The real query I want to do would have 4+ sub queries, which I know probably wouldn't be too ideal, performance wise. Or is it okay to multiple subqueries?

 

Link to comment
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.