简体   繁体   中英

SQL (How do I display all results from all tables?)

I would love if someone could help me with this.

I have 3 tables

Table1 Table2 Talbe3

(They are almost the same, but however some columns are slightly different)

What I wanted to ask you is how do I display all results from all 3 tables (including rows and columns) on a webpage.

Also how do I make the search bar look through all 3 tables.

For example, if I someone searches by First name or Last Name ("An") So the results display rows from all 3 tables that contain "An" for example (Annete, Andrew, Anamari, Andrea) etc.

Thank you

Try UNION: If your three tables have the same number of columns, then you could do something like this:

SELECT Table1.* FROM Table1 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%' UNION SELECT Table2.* FROM Table2 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%' UNION SELECT Table3.* FROM Table3 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%'

If Not, you can specify the fields you need:

SELECT FirstName, LastName FROM Table1 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%' UNION SELECT FirstName, LastName FROM Table2 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%' UNION SELECT FirstName, LastName FROM Table3 WHERE FirstName LIKE 'An%' OR LastName LIKE 'An%'

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