Php 在联接查询中基于表名区分记录

Php 在联接查询中基于表名区分记录,php,mysql,join,Php,Mysql,Join,我正在使用MySQL中的join查询从多个表中获取记录。 我想做的是根据表名区分记录 比如说, 1)Table1 name test1 test2 2)Table2 name test3 test4 join之后,它会给我所有这样的记录 name test1 test2 test3 test4 我只想要这样的输出 name from_table test1 table1 test2 table1 test3 table2 test4 table2 最好的方法是什

我正在使用
MySQL
中的
join
查询从多个表中获取记录。 我想做的是根据表名区分记录

比如说,

1)Table1

name
test1
test2

2)Table2

name
test3
test4
join
之后,它会给我所有这样的记录

name
test1
test2
test3
test4
我只想要这样的输出

name    from_table
test1   table1
test2   table1
test3   table2
test4   table2
最好的方法是什么?
提前感谢。

您不需要联接,可以使用UNION ALL查询和常量列:

SELECT name, 'table1' AS fromtable
FROM table1
UNION ALL
SELECT name, 'table2' AS fromtable
FROM table2

您不需要联接,可以使用UNION ALL查询和常量列:

SELECT name, 'table1' AS fromtable
FROM table1
UNION ALL
SELECT name, 'table2' AS fromtable
FROM table2