简体   繁体   中英

How to Make Multiple Select with a Common Where Clause in Single Query?

How could I create a single query for all values from more than 1 tables where a county is equal to 'baawa'. I am using PHP to connect to the database.

What I tried:

    SELECT (                
            SELECT * FROM health WHERE county='baawa'
            ),
            (
            SELECT * FROM hotspots WHERE county='baawa'
            ),
            (
            SELECT * FROM markets WHERE county='baawa'
            ),
            (
            SELECT * FROM schools WHERE county='baawa'
            ),
            (
            SELECT * FROM security WHERE county='baawa'
            )

Edit: The tables have different number of columns.

Tables columns:

Health facility_id, name, ward, location, lat, lon, staff, staff_category, structure, emergencies, conflict_cases, communication_facility, power


Hotspots name, lat, lon, ward, location, mgt_committee, security, type


Market market_id, name, ward, location, lat, lon, mgt_committee, security


Schools Full texts, school_id, name, level, ward, location, lat, lon, students, staff_no, peace_club, conflict_affected


Security security_id, name, ward, location, lat, lon, staff, structure, transport, communication, power, crimes, resolutions

Since SQL queries return 2-dimensional rowsets, the output must have a consistent set of columns. It is therefore not possible to issue one query to several significantly different tables, and produce output that includes all columns for all involved tables see footnote . If you want all columns to be available such as SELECT * would produce, you will need to issue 5 separate queries and fetch the rows from the individually.

What can be done with your existing structure:

With your existing structure, to get a combined rowset of all these identical, you will need to chain them together with UNION ALL using the pattern:

SELECT col1, col2 FROM health WHERE county = 'baawa'
UNION ALL
SELECT col1, col2 FROM hotspots WHERE county = 'baawa'
UNION ALL
SELECT col1, col2 FROM markets WHERE county = 'baawa'
UNION ALL....
SELECT col1, col2 FROM schools WHERE county = 'baawa'
UNION ALL
SELECT col1, col2 FROM security WHERE county = 'baawa'
/* ORDER BY applies to the whole rowset */
ORDER BY col2 DESC, col1 ASC

The catch is that you can only get columns which are common to all tables using this method in the simplistic way I have done here. Looking over your table structures, the common columns I see are name, ward, location, lat, lon . So you must list them in the same order in each UNION component's SELECT .

Notice that I do not use SELECT * on a UNION query. Unless the tables are exactly the same, having had the columns defined in exactly the same order originally, SELECT * will not produce the columns in the same order and your output won't make sense because they don't align across the UNION 'd tables.

When doing a UNION query it is important to be explicit about the order of the columns in the SELECT list. In your case, this would not be possible anyway because the tables differ significantly.

If you needed to include one or two other columns in the resultant query which are not common to all tables, you can do it in the SELECT list by adding a NULL in the UNION components where the column does not exist. For example mgt_committee column for the 2 where it exists, and NULL where it doesn't:

/* NULL as mgt_committee where column doesn't exist in table... */
SELECT col1, col2, NULL AS mgt_committee FROM health WHERE county = 'baawa'
UNION ALL
SELECT col1, col2, mgt_committee FROM hotspots WHERE county = 'baawa'
UNION ALL
SELECT col1, col2, mgt_committee FROM markets WHERE county = 'baawa'
UNION ALL
SELECT col1, col2, NULL AS mgt_committee FROM schools WHERE county = 'baawa'
UNION ALL
SELECT col1, col2, NULL AS mgt_committee FROM security WHERE county = 'baawa'

Footnote:

The only way to include all columns from all tables in one SELECT would be to list the superset of all of them as I did with mgt_committee , meaning all columns from all tables must be represented in each component of the UNION , with NULL replacing the columns that don't exist. Don't do this, it doesn't make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM