繁体   English   中英

从MySQL上两个不同的表中获取行,两个表之间没有任何关系

[英]fetch rows from two different table on MySQL without any relation between both tables

有两个表,两个表之间也没有关系,

结构:table_one

+---------+--------+-----------+
| name    | age    |insertTEMP |
+---------+--------+-----------+
| cat     | 12     |02-02-2014 |
| dog     | 13     |03-04-2014 |
+---------+--------+-----------+

结构:table_two

+---------+--------+---------------+
| book    | pages  | insertTEMP    |
+---------+--------+---------------+
| book1   | 34     | 05-02-2014    |
| book2   | 54     | 04-03-2014    |
+---------+--------+---------------+

现在的想法是在这里我想从两个表中列出记录,所以我保持波纹管功能以列出它们。

public function recentALL() {
   $sql = "select * from table_one, table_two ORDER BY insertTEMP";
    $q = $this -> conn -> prepare($sql);
    $q -> execute();
    return $row = $q -> fetchAll();
 }

我从视图中获取它,

        $data2 = $functions->recentAll();
            foreach ($data2 as $data3){
                  echo $data3['name']  . $data3['book'] . '<br />';

           }

我如何使其显示为:

+---------+--------+
| cat     | NULL   |
| null    | book1  |
| null    | book2  |
| dog     | null   |
+---------+--------+

这是基于insertTEMP

使用以下查询

Select * from (
    Select name  as Name, null as Book,insertTEMP  from table_one
    UNION ALL
    select null as Name, book as Book,insertTEMP  from table_two
) as tmp
Order by tmp.insertTEMP asc

根据查询更改代码中的列名

暂无
暂无

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

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