MYSQL语句,union生成一个表无法update,求高手帮忙,谢谢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL语句,union生成一个表无法update,求高手帮忙,谢谢相关的知识,希望对你有一定的参考价值。
1.create view test as (语句一) union (语句而),这个时候view无法update
The target table test of the UPDATE is not updatable
2.但是 create view1 test as (语句一)时候,view1可以update,
3.create view test2 as (语句二)时候,view2也可以Update
所以基本确定是这个union的问题,求高手指出问题,以及解决办法。。。
需要把两个表联合成一个表并用触发器update。。。但是Union后死活update不了。。
谢谢楼下的回答,但是我的union又不是union all,所以根本不会有重复的语句。。。。
union成一个视图是必须的了(用作一个用户表),但是,莫非如果我想要update里面的数据,就一定要回到原来的表?????????????
谢谢
如果想让view 可以更新,建view 语句需要满足以下条件
没有 表结合
没有 集合演算 如 UNION ALL / INTERSECT / MINUS
没有 GROUP BY
没有 集合函数
没有 DISTINCT本回答被提问者采纳 参考技术B 多表view,更新有限制,这是mysql手册上的 内容:
在某些情况下,能够更新多表视图,假定它能使用MERGE算法进行处理。为此,视图必须使用内部联合(而不是外部联合或UNION)。此外,仅能更新视图定义中的单个表,因此,SET子句必须仅命名视图中某一表的列。即使从理论上讲也是可更新的,不允许使用UNION ALL的视图,这是因为,在实施中将使用临时表来处理它们。
参考资料:http://dev.mysql.com/doc/refman/5.1/zh/views.html
参考技术C 因为你的视图跨了多个表,所以不能更新了,试想一下,如果你能在这个视图上插入一条记录保存后,数据库如何知道记录实际上要被存进那个实体表中,因为视图本身并不是一个存储体。所以只好在触发器中写两条update语句分别更新两个表了。 参考技术D 因为你的视图跨了多个表,所以只能用于查询,如果要删除的 你可以简称一个表的形式 create table test as (select * from test2) 第5个回答 2019-03-18 直接修改表里的数据,试图自然就可以更新了
Mysql union all select问题
【中文标题】Mysql union all select问题【英文标题】:Mysql union all select issue 【发布时间】:2017-01-10 21:49:06 【问题描述】:我正试图找出我哪里出错了。我正在使用不同的 Where 语句从同一个表中加入多个选择语句,其中一个选择也是一个连接。我正在尝试使用 union all 但我似乎遗漏了一些东西,我无法弄清楚它是什么。任何帮助都会很棒。以下是我的代码:
SET @LASTQTR:=IF((QUARTER(CURDATE( ))-1) = 0, 4, QUARTER(CURDATE( ))-1);
SET @YR:=IF(@LASTQTR = 4, YEAR(NOW( ))-1, YEAR(NOW( )));
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
UNION ALL
(SELECT COUNT(id) AS external FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id)
UNION ALL
(SELECT COUNT(id) AS internal FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id)
ORDER BY total DESC LIMIT 10;
我从这个查询中寻找的最终结果是在 while 循环中使用结果,如下所示:
while ( $re = $q8->fetch(PDO::FETCH_ASSOC))
echo'<tr>
<td style="width:40%;">'.$re['fromName'].'</td>
<td class="text-right">'.$re['external'].'</td>
<td class="text-right">'.$re['internal'].'</td>
<td class="text-right">'.$re['total'].'</td>
</tr>';
【问题讨论】:
【参考方案1】:所以我可能会在这种情况下帮助其他人,这就是我正在寻找的结果:
SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName,
(SELECT COUNT(id) FROM Referrals WHERE rtype=1 AND from_id=r.from_id GROUP BY fromName) AS external,
(SELECT COUNT(id) FROM Referrals WHERE rtype=2 AND from_id=r.from_id GROUP BY fromName) AS internal
FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY fromName ORDER BY total DESC LIMIT 10
【讨论】:
【参考方案2】:对于联合,所有集合都应该具有相同的列。在您的情况下,第一个查询也有 fromName 。要么删除它,要么在其他查询中放置占位符
SET @LASTQTR:=IF((QUARTER(CURDATE( ))-1) = 0, 4, QUARTER(CURDATE( ))-1);
SET @YR:=IF(@LASTQTR = 4, YEAR(NOW( ))-1, YEAR(NOW( )));
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
UNION ALL
(SELECT COUNT(id) AS external,'' FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id)
UNION ALL
(SELECT COUNT(id) AS internal,'' FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id)
ORDER BY total DESC LIMIT 10;
根据您的评论(尚未执行此操作)
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName,
(SELECT COUNT(id) FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id) AS external,
(SELECT COUNT(id) FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id) AS internal
FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
【讨论】:
使用上述方法的这个问题是,当循环遍历结果时,它将每个 select 语句作为一个新行放入 while 循环中。我认为使用 Union all 会返回每行所有三个选择的结果。以上是关于MYSQL语句,union生成一个表无法update,求高手帮忙,谢谢的主要内容,如果未能解决你的问题,请参考以下文章