gpt4 book ai didi

php - 在连接查询中根据表名区分记录

转载 作者:行者123 更新时间:2023-11-29 04:00:09 25 4
gpt4 key购买 nike

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

例如,

1)Table1

name
test1
test2

2)Table2

name
test3
test4

加入后,它会给我这样的所有记录,

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

关于php - 在连接查询中根据表名区分记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com