1

I have a set of database tables named like:

site_1_details
site_2_details
site_3_details
...
site_420_details

all tables have the same fields, like:

ID  | SETTING | VALUE 
----------------------
1     name      Site 1 Name
2     desc      Site 1 Desc
3     email     Site 1 Email...
...

(only the value fields are different)

How can I get a set of values from certain tables?

For example, I want to get the name & email values from sites 3,7 and 15. How could I do that with a SQL query?

1 Answer 1

4
SELECT 3 AS siteID, name, email
FROM site_3_details

UNION

SELECT 7 AS siteID, name, email
FROM site_7_details

UNION

SELECT 15 AS siteID, name,email
FROM site_15_details

This is a horribly bad design. Why couldn't you put a "siteID" field into a single table, which'd reduce the query to:

SELECT name, email
FROM site_details
WHERE siteID IN (3,7,15);

comment followup:

Ah well, then you just modify the individual queries:

SELECT 7 AS siteID, ID as fieldID, name AS fieldName
FROM site_7_details WHERE SETTING IN ('name', 'email')

UNION

....

Any reason you've designed the tables like this? Sounds like you're trying to implement your own database on TOP of a database engine which is already perfectly suited to doing this kind of relational data handling.

Sign up to request clarification or add additional context in comments.

3 Comments

but name and email are not fields, they are records. does select work like this with records? The fields are ID/Settings/Value
but if I would use a single table for all this, this table would turn out to be huge
And? That's what databases are for - selecting specific data based on your requirements. There's MySQL databases out there with tables with literally billions of rows. A few hundred or few thousand rows is NOTHING.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.