We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Need help with challenging MySQL query!

Development and Programming

Clive Portman's avatar
Clive Portman
83 posts
15 years ago
Clive Portman's avatar Clive Portman

I’m sure someone with twice my brain power can help me out here…

I have five columns called Region1, Region2, all the way to Region5. I have five more columns called Location1, Location2 and so on, which correspond to the same numbered Region column.

I’m trying to write a SELECT query which returns a list of discrete values from all five location columns, but only if the corresponding region column contains a certain value.

For example, I might want to fetch a list of all the discrete locations where the corresponding Region value is ‘Southern France’, even though Southern France could exist in one or more of those Region columns.

Anyone?

       
Adrian Macneil (Crescendo)'s avatar
Adrian Macneil (Crescendo)
28 posts
15 years ago
Adrian Macneil (Crescendo)'s avatar Adrian Macneil (Crescendo)

To be honest, it sounds like you need to look a bit closer at your database design. Could you give more of an idea of what is in your database table, and why you need to have region1..5 corresponding to location1..5 as columns?

Each row in your database should contain information relating only to one particular thing (the primary key). Different (non-primary key) columns in the table should never hold information about other columns, for example having region1 related to location1 and region2 related to region2 inside your table would be considered bad design, it would be most likely better to have a separate table linking a region and location to your original table (and you could use php code to ensure no more than 5 get entered if that is what you wish). But I don’t want to second guess your design, more information would be helpful.

If you have a good reason for the existing layout, then could you do something like ‘select * from tablename where region1 == ‘southern france’ OR region2 == ‘southern france’ OR region3… etc?

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.