首页 > 编程知识 正文

与mysql跨平台跨数据库操作(sql server跨数据库)

时间:2023-12-16 10:45:31 阅读:316237 作者:DOHC

本文目录一览:

mysql如何实现跨数据库查询并按where子

1、where型子查询

(把内层查询结果当作外层查询的比较条件)

#不用order by 来查询最新的商品

select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);

#取出每个栏目下最新的产品(goods_id唯一)

select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id);

2、from型子查询

(把内层的查询结果供外层再次查询)

#用子查询查出挂科两门及以上的同学的平均成绩

思路:

#先查出哪些同学挂科两门以上

select name,count(*) as gk from stu where score 60 having gk =2;

#以上查询结果,我们只要名字就可以了,所以再取一次名字

select name from (select name,count(*) as gk from stu having gk =2) as t;

#找出这些同学了,那么再计算他们的平均分

select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu having gk =2) as t) group by name;

3、exists型子查询

(把外层查询结果拿到内层,看内层的查询是否成立)

#查询哪些栏目下有商品,栏目表category,商品表goods

select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);

MySQL数据库如何跨数据库插入数据,我现在要同时向2个库的2个表插入相同记录

连接不同的数据库只能使用不同的数据连接。

你再建一个数据连接到另外一个库,执行插入。

如何让mysql远程连接其他数据库

一、连接远程数据库:

1、显示密码

如:MySQL

连接远程数据库(192.168.5.116),端口“3306”,用户名为“root”,密码“123456”

C:/mysql -h

192.168.5.116 -P 3306 -u root -p123456

2、隐藏密码

如:MySQL 连接本地数据库,用户名为“root”,

C:/mysql -h

localhost -u root -p

Enter password:

二、配置mysql允许远程链接

默认情况下,mysql帐号不允许从远程登陆,只能在localhost登录。本文提供了二种方法设置mysql可以通过远程主机进行连接。

一、改表法

在localhost登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host"

项,将"localhost"改称"%"

例如:

#mysql -u root

-p

Enter password:

……

mysql

mysqlupdate user

set host = '%' where user = 'root';

mysqlselect host,

user from user;

二、授权法

例如:

你想myuser使用mypassword(密码)从任何主机连接到mysql服务器的话。

mysqlGRANT ALL

PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码

mysqlGRANT ALL

PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3'IDENTIFIED BY

'mypassword' WITH GRANT OPTION;

mysqlFLUSH

PRIVILEGES

使修改生效,就可以了

常见问题:

1、在采用法二授权法之后,无法在本地登录mysql(如:#mysql -u root -p -h

192.168.5.116

Enter password:

ERROR 1045 (28000): Access denied for user

'root'@'loadb116' (using password: YES)

上例中loadb116是主机名.

解决方法:

1、这时可以使用:mysql -u

root -p 登录,进入到mysql后。

mysql grant all privileges on *.* to 'root'@'loadb116'

identified by '123456' with grant option;

Query OK, 0 rows affected

(0.00 sec)

mysql flush

privileges;

Query OK, 0 rows affected (0.00

sec)

2、在本地使用ip地址登录

#

mysql -u root -p -h

192.168.5.116

Enter password:

Welcome to the MySQL

monitor. Commands end with ; or /g.

Your MySQL connection id is 60

Server

version: 5.1.45 MySQL Community Server (GPL)

Type 'help;' or '/h' for

help. Type '/c' to clear the buffer.

mysql

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。