简体   繁体   English

MYSQL / PHP-合并具有不同列的两个表

[英]MYSQL / PHP - merge two tables with different columns

I want to access a database merging two tables from a php script in order to output the result to an html table. 我想从php脚本访问合并两个表的数据库,以便将结果输出到html表。 Both tables are pretty much the same except one column I need only exists in one table. 这两个表几乎相同,除了我只需要在一个表中存在一列。 I don't have direct access to the database. 我没有直接访问数据库的权限。

INSERT INTO globalusers
(surname, firstname, email, location)

INSERT INTO localusers
(surname, firstname, email)

Location exists only in globalusers. 位置仅存在于globalusers中。 I need to use this column as a dropdown filter in my table and add the column location with the string "local" to the data coming from the localusers table. 我需要将此列用作表中的下拉过滤器,并将包含字符串“ local”的列位置添加到来自localusers表的数据中。

Here's a fiddle 这是一个小提琴

So the expected result would be one table with four columns and the string "local" in the location column for the localusers table. 因此,预期结果将是一个包含四列的表,并且在localusers表的location列中包含字符串“ local”。

What is the select that I need? 我需要什么选择?

MySQL version: 5.5.36 MySQL版本:5.5.36

Sounds like you're looking to combine the results of the two tables. 听起来您想合并两个表的结果。 If so, you can use UNION ALL for this: 如果是这样,您可以为此使用UNION ALL

SELECT surname, firstname, email, location
FROM globalusers
UNION ALL
SELECT surname, firstname, email, 'local'
FROM localusers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM