LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   MySQL combined query challenge (https://www.linuxquestions.org/questions/programming-9/mysql-combined-query-challenge-475726/)

firemankurt 08-20-2006 08:32 PM

MySQL combined query challenge
 
I have a table to hold all my program settings and I am trying to figure out how to combine two queries as follows.
Here is table named `Config`.
Code:

------------------- Config Table ---------------------
|Setting | Param1    | Param2    | Param3          |
======================================================
|Access  | Page1    |            | ABCD            |
|Access  | Page2    |            | D                |
|Access  | Page3    |            | AD              |
|Access  | Page4    |            | ABC              |
|ServRoot|          |            | /MyServer/root/  |
|Menu    | Page1    | php        | The Main Page    |
|Menu    | Page2    | php        | Photo Gallery    |
|Menu    | Page3    | php        | Members Entrance |
|Menu    | Page4    | htm        | Some Static Page |
------------------------------------------------------

After the User logs in and is given an access level I want to build a custom menu.

- Using the 'Settings' table I check access rights for each page based on a predetermined acess level listed in 'Param3'.

If the user access level is 'B' I would do:
Code:

"SELECT `Param1` FROM `Config` WHERE `Setting` = 'Access' AND `Param3` LIKE '%B%';"
- Then I recheck the 'Settings' table to get all the Menu items they have access to. I use the results from the first query to build a comma seperated string. The results from the first query woould build "('Page1','Page4')".
Code:

"SELECT * FROM `Config` WHERE `Setting`='Menu' AND `Param1` IN ('Page1','Page4');"
Code:


--------------------Result------------------------
|Setting | Param1    | Param2 | Param3          |
==================================================
|Menu    | Page1    | php    | The Main Page    |
|Menu    | Page4    | htm    | Some Static Page |
--------------------------------------------------

How can I combine these queries to get the same result?

jiml8 08-20-2006 09:46 PM

You can't because your database table 'config' is multi-purpose, defining access types, data paths, and menu contents.

To do what you want to do, split the config table into multiple tables, one used to define access, one to do datapaths (or just put that in a flatfile), and one to set up menu contents. Then you can do it all in one multi-table query using joins.

firemankurt 08-21-2006 12:07 AM

Thanks for your reply. I did have all this in a "config.php" flat file as arrays and I am trying to move away from that into something easier to manage and easier for people to install without havin to edit PHP files.

I plan on using an interview type install technique and fill in the settings from the answers they give through web forms.

I was hoping I could store it all in one table but I am begining to feel the pain of this idea.

aluser 08-21-2006 01:59 AM

I agree with the other posters that you want separate tables. But, if you're stubborn,
Code:

sqlite> SELECT * FROM Config;
Access|Page1||ABCD
Access|Page2||D
Access|Page3||AD
Access|Page4||ABC
ServRoot|||MyServer/root/
Menu|Page1|php|The Main Page
Menu|Page2|php|Photo Gallery
Menu|Page3|php|Members Entrance
Menu|Page4|htm|Some Static Page
sqlite> SELECT * FROM Config WHERE Setting = 'Menu' AND Param1 IN ( SELECT Param1 FROM Config WHERE Setting = 'Access' AND Param3 LIKE '%B%' );
Menu|Page1|php|The Main Page
Menu|Page4|htm|Some Static Page



All times are GMT -5. The time now is 01:26 PM.