php - 从多个表中抓取行作为单个结果?

标签 php mysql

我有 2 个表。表 1 有字段 A、B、C、D,表 2 有字段 A、B。两个表的字段 A 和 B 具有相同的记录类型。我想从字段 A 和 B 的两个表中获取记录作为单个结果。

PHP+MySql中有Query或者Function吗?

谢谢...

最佳答案

SQL 中有一个联合子句可以满足您的需求:

select a,b from table1
    where <where-clause>
union all select a,b from table2
    where <where-clause>

或者,如果您想要所有字段(table2 的空格):

select a,b,c,d from table1
    where <where-clause>
union all select a,b,' ' as c,' ' as d from table2
    where <where-clause>

第二个查询中的空格可能需要扩展以适应 c 和 d 的字段大小。

关于php - 从多个表中抓取行作为单个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/176774/

相关文章:

MySQL 统计组数

php - PayPal 捐赠与 laravel 5.1 集成

php - 如何强制 wordpress 中的特定页面为非 SSL (http)

php - 检查上传文件的最可靠方法是图像

php - Curl 不写入文件 cookie

java - 为什么我尝试用 java 解密 RSA 但用 php 加密时会出现此异常?

PHP : Can retrieve data from db but can not insert

mysql - 无法使用 Weka 连接到 MySQL

mysql - 在MySql工作台中从* .sql创建数据库?

mysql - 为什么将数据拆分到多表中并不能提高性能?