如何使用 JOIN 在 mysql 中查看视图?

Posted

技术标签:

【中文标题】如何使用 JOIN 在 mysql 中查看视图?【英文标题】:How can I make view in mysql using JOIN? 【发布时间】:2016-12-28 14:43:03 【问题描述】:

有两个表,table1 和 table2。

[表1]

+----+------+
| no | time |
+----+------+
| 1  | 1111 |
+----+------+
| 2  | 2222 |
+----+------+
| 3  | 3333 |
+----+------+
| 4  | 4444 |
+----+------+
| 5  | 5555 |
+----+------+

[表2]

+----+-----+----------+------+
| no | idx | name     | rank |
+----+-----+----------+------+
| 1  | 1   | Apple    | 1    |
+----+-----+----------+------+
| 2  | 1   | Banana   | 2    |
+----+-----+----------+------+
| 3  | 1   | Car      | 3    |
+----+-----+----------+------+
| 4  | 1   | Dragon   | 4    |
+----+-----+----------+------+
| 5  | 1   | Eagle    | 5    |
+----+-----+----------+------+
| 6  | 2   | Fire     | 2    |
+----+-----+----------+------+
| 7  | 2   | God      | 3    |
+----+-----+----------+------+
| 8  | 2   | Hippo    | 4    |
+----+-----+----------+------+
| 9  | 3   | Icecream | 1    |
+----+-----+----------+------+
| 10 | 3   | Juice    | 3    |
+----+-----+----------+------+
| 11 | 3   | Korea    | 4    |
+----+-----+----------+------+
| 12 | 3   | Low      | 5    |
+----+-----+----------+------+
| 13 | 4   | Mother   | 2    |
+----+-----+----------+------+
| 14 | 4   | News     | 3    |
+----+-----+----------+------+
| 15 | 5   | Object   | 1    |
+----+-----+----------+------+

而且,我想使用 mysql 制作一个类似跟随图像的视图。

+----+-----+----------+------+------+
| no | idx | name     | time | rank |
+----+-----+----------+------+------+
| 1  | 1   | Apple    | 1111 | 1    |
+----+-----+----------+------+------+
| 2  | 1   | Banana   | 1111 | 2    |
+----+-----+----------+------+------+
| 3  | 1   | Car      | 1111 | 3    |
+----+-----+----------+------+------+
| 4  | 1   | Dragon   | 1111 | 4    |
+----+-----+----------+------+------+
| 5  | 1   | Eagle    | 1111 | 5    |
+----+-----+----------+------+------+
| 1  | 2   | Apple    | 2222 | 1    |
+----+-----+----------+------+------+
| 6  | 2   | Fire     | 2222 | 2    |
+----+-----+----------+------+------+
| 7  | 2   | God      | 2222 | 3    |
+----+-----+----------+------+------+
| 8  | 2   | Hippo    | 2222 | 4    |
+----+-----+----------+------+------+
| 5  | 2   | Eagle    | 2222 | 5    |
+----+-----+----------+------+------+
| 9  | 3   | Icecream | 3333 | 1    |
+----+-----+----------+------+------+
| 6  | 3   | Fire     | 3333 | 2    |
+----+-----+----------+------+------+
| 10 | 3   | Juice    | 3333 | 3    |
+----+-----+----------+------+------+
| 11 | 3   | Korea    | 3333 | 4    |
+----+-----+----------+------+------+
| 12 | 3   | Low      | 3333 | 5    |
+----+-----+----------+------+------+
| 9  | 4   | Icecream | 4444 | 1    |
+----+-----+----------+------+------+
| 13 | 4   | Mother   | 4444 | 2    |
+----+-----+----------+------+------+
| 14 | 4   | NEws     | 4444 | 3    |
+----+-----+----------+------+------+
| 11 | 4   | Korea    | 4444 | 4    |
+----+-----+----------+------+------+
| 12 | 4   | Low      | 4444 | 5    |
+----+-----+----------+------+------+
| 15 | 5   | Object   | 5555 | 1    |
+----+-----+----------+------+------+
| 13 | 5   | Mother   | 5555 | 2    |
+----+-----+----------+------+------+
| 14 | 5   | News     | 5555 | 3    |
+----+-----+----------+------+------+
| 11 | 5   | Korea    | 5555 | 4    |
+----+-----+----------+------+------+
| 12 | 5   | Low      | 5555 | 5    |
+----+-----+----------+------+------+

rank 列的范围是 1 到 5。

而且,如果没有像 (idx2 & rank5) 这样的数据,那么我想使用与该数据具有相同 rank 和最大 idx 和低于 idx 的数据。

我尝试使用以下查询。

select * from (`table1` JOIN (select 1 AS `rank` union select 2 union select 3 union select 4 union select 5) AS `x` )

但是,我不知道以后该怎么办。

【问题讨论】:

你应该发布文本数据而不是图片 @GurwinderSingh 但是,我无法使用文本制作表格:( 你有没有尝试过做任何事情? 你可以。 Look @GurwinderSingh 我已经改变了我的问题。谢谢你的建议。 【参考方案1】:

试试这个

select ifnull(t2a.no, t2b.no) no, t.idx, ifnull(t2a.name, t2b.name) name, t.rank
from (
    select t3.idx, t1.no rank, (select max(idx) from table2 where rank = t1.no and idx<t3.idx) max_idx
    from (select distinct idx from table2 ) t3
    cross join table1 t1    
) t 
 left join table2 t2a on t2a.rank=t.rank and t.idx  = t2a.idx
 left join table2 t2b on t2b.rank=t.rank and t.max_idx = t2b.idx
order by 2,4

【讨论】:

我很抱歉,但它不起作用。结果是这样的。 imgur.com/a/Em5Hj

以上是关于如何使用 JOIN 在 mysql 中查看视图?的主要内容,如果未能解决你的问题,请参考以下文章

使用 LEFT JOIN 的 MySQL 视图中的问题... GROUP BY

如何用 MySQL 实现物化视图?

mysql的unionleft join right join inner join和视图学习

sql 如何查看视图的创建用户?

如何查看用于在 phpMyAdmin 或其他程序中创建视图的原始 MySQL?

MySQL高级