T
T
timofy2019-10-03 17:59:42
MySQL
timofy, 2019-10-03 17:59:42

How to get values ​​from several MySQL tables so that as a result it is possible to determine from which table which values ​​were extracted?

How to get values ​​​​from 3 MySQL tables, so that as a result you can determine from which table which values ​​\u200b\u200bare extracted?
This option is not suitable:

SELECT id
    FROM table1
    WHERE name= 'Tom'
    UNION 
    SELECT id
    FROM table2
    WHERE name= 'Tom'
    UNION 
    SELECT id
    FROM table3
    WHERE name= 'Tom'

since it combines all the found values ​​into one array, but you need to know as a result of the query which values ​​were retrieved from which table

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey c0re, 2019-10-03
@timofy

SELECT 'table1' as tabl, id
  FROM table1
  WHERE name= 'Tom'
UNION 
SELECT 'table2' as tabl, id
  FROM table2
  WHERE name= 'Tom'
UNION 
SELECT 'table3' as tabl, id
  FROM table3
  WHERE name= 'Tom'

N
netyshka, 2019-10-03
@isset89

the question is not clear at all, but I'll take a chance:
group_concat(distinct a.id )AS table1_id, ifnull(group_concat(distinct b.id), '-') AS table2_id, group_concat( distinct c.id) AS table3_id
from table1 a
left join table2 b on a.name = b.name
left join table3 c on c.name = b.name or a.name=c.name
where a.name='tom' or b.name ='tom' or c.name ='tom'
right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question