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
https://forums.phpfreaks.com/topic/140632-solved-is-a-subquery-the-only-way/
Share on other sites

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.