Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于数据管理的MySQL程序编写_Mysql_Sql_Stored Procedures - Fatal编程技术网

用于数据管理的MySQL程序编写

用于数据管理的MySQL程序编写,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我在MySQL中有db,有两个表,我需要这样进行查询 SELECT a.*, b.* from db1.A a1 left join db2.A a2 using(id) where a1.email <> a2.email 结果我想得到 db1.A db2.A email email www@ww.ww www@ww.ww 我可以用任何脚本语言编写,但是可以用仅帮助SQL完成(这样的任务)吗?使用表达式!当第一个参数为NULL时,它

我在MySQL中有db,有两个表,我需要这样进行查询

SELECT a.*, b.* from db1.A a1 left join db2.A a2 using(id) where a1.email <> a2.email
结果我想得到

db1.A          db2.A
email          email
www@ww.ww      www@ww.ww
我可以用任何脚本语言编写,但是可以用仅帮助SQL完成(这样的任务)吗?

使用表达式!当第一个参数为
NULL
时,它使用第二个参数

大概是这样的:

SELECT a.*, ISNULL(b.columnName, a.columnName) as 'columnName'
from db1.A a1
left join db2.A a2 using(id)
where a1.email <> a2.email
选择a.*,ISNULL(b.columnName,a.columnName)作为“columnName”
来自db1.a1
使用(id)左键连接db2.a2
其中a1.email a2.email


您必须将
列名
替换为真实的列名,并对所需的每一列执行此操作。

是否尝试使用is not null条件

SELECT a.*, b.* 
from db1.A a1 
left join db2.A a2 
using(id)
 where a1.email <> a2.email
and (a1.mail is not null and a2.mail is not)
选择a.*和b.*
来自db1.a1
左键连接db2.a2
使用(id)
其中a1.email a2.email
和(a1.mail不为空,a2.mail不为空)

您需要更新语句

Mysql对更新使用了一种稍微不标准的语法(我有忘记的倾向)。我认为正确的语法是:

update db2
    from db1
    set db2.email = db1.email
    where db1.id = db2.id and (db2.email is null or db2.email <> db1.email)
更新db2
从db1开始
设置db2.email=db1.email
其中db1.id=db2.id和(db2.email为null或db2.email db1.email)

在我看来,下一个查询很容易实现:

update db2.A a2, db1.A a1 set a2.email=a1.email where a1.id=a2.id and (db2.email is null or db2.email = '') and a1.email <> a2.email;
更新db2.aa2,db1.aa1集a2.email=a1.email,其中a1.id=a2.id和(db2.email为null或db2.email='')和a1.email a2.email;

谢谢大家。

在那份声明中,您从未提到过TableName
update db2.A a2, db1.A a1 set a2.email=a1.email where a1.id=a2.id and (db2.email is null or db2.email = '') and a1.email <> a2.email;