The database has 2 tables with the same fields. For example, in Table1 there is a field NAME, and in Table2 there is a field NAME. How can I display all the data from both tables, for which the value of the field NAME = FizLico?
- oneIn the question, give the structure of the tables and what you want to receive - Anton Shchyrov
- How to withdraw? If "first data from one table, then from another", then for this the number and types of output columns in the table must match. - Regent
|
4 answers
Not quite clear what you need to withdraw.
You can try UNION (provided that the columns in table 1 and table 2 are the same):
select * from ( select * from table1 where name = 'FizLico' UNION /*либо UNION ALL при необходимости сохранения дубликатов*/ select * from table2 where name = 'FizLico') b order by client - Rolled like this select * from ip where name = 'FizLico' UNION ALL select * from ip_14 where name = 'FizLico' - Obloko
- and how then to sort the output by the client, ORDER BY field in a simple query from one table, and from two? - Obloko
- @Obloko corrected the answer. If the answer turned out to be the right one for you - do not forget to tick it opposite. - Denis
- error 1248 - Observedly
- @Obloko corrected. - Denis
|
Poorly clear what you need ... try to start with this:
SELECT * FROM table1, table2 WHERE table1.name = table2.name AND table1.name = 'FizLico'; - Oh thank you! frankly honest, probably the first clear example I met! - Dimastik86
|
SELECT column_name1, column_name2, ... FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name WHERE table1.NAME = 'FizLico' |
To get data from several tables, you need some kind of key by which you will identify the value you need and the connection between the tables takes place with the help of Inner Join - SQL Joins
Example:
SELECT <fields> FROM tbl1 INNER JOIN tbl2 ON tbl1.<field>=tbl2.<field> |