B
B
boss1rich2016-06-10 17:13:04
MySQL
boss1rich, 2016-06-10 17:13:04

How to form an sql query to search multiple tables at the same time?

Greetings. I ask for help in the banal question of compiling an SQL query to select data from the database.
We have 3 tables with goods:
table1 - contains a group of goods 1
table2 - a group of goods 2
table3 - a group of goods 3
Each table has a SAP field, which contains an article for each specific product.
Question:
How to generate an sql-query to search for a product in three tables at once using its SAP code? There are 3 tables, but the product you need is only in one of them.
Thanks everyone for the help!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SofroN, 2016-06-10
@SofroN

use union

select sap from table1
union
select sap from table2
union
select sap from table3

https://ru.wikipedia.org/wiki/Union_(SQL)

D
DuD, 2016-06-10
@DuD

And you can also combine them into one view and select from one as from a usual table.

A
Andrey, 2016-06-10
@VladimirAndreev

select * from
(
select * from t1
union all select * from t2
union all select * from t3 ) a
where a.SAP=100500

B
boss1rich, 2016-06-11
@boss1rich

Gentlemen, maybe you will have a solution or at least point in the right direction?
Ps
all three tables have a different number of fields, but in each of them there is a field with the product identifier - the SAP field. According to the UNION ALL manuals, tables should return the same number of columns and compatible data types in the corresponding columns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question