MySQL不同表查詢,不同資料庫查詢

2021-05-27 13:21:52 字數 1078 閱讀 9012

(內容比較弱~歡迎大神們指點~)

在mysql中~要實現不同表的查詢和不同資料庫的查詢,首先得有地方查吧

1、建立資料庫

create databaes test;

use test;

create table pet(id int,name varchar(20));

create table user(id int,username varchar(20));

create database huihui;

use huihui;

create table user(id int,name varchar(20));

到這裡,我們的資料庫和表就建好了.接下來要在表中插入各種資料。

以上的select語句就是查詢輸出資料庫中全部內容的語句:select * from tablename;

2、下面進行同乙個資料庫中不同表的查詢

比如我要查詢test資料庫中pet和user中id相同的資料

先要使用 use test;把資料庫切換到test。

select pet.*,user.username from pet,user where pet.id=user.id;

select語句後面可以加order by id,這樣就可以按照id來排序了。

3.下面進行不同資料庫中不同表的查詢

比如我要查詢test資料庫中pet和huihui資料庫中的user中id相同的資料

因為是查詢不同資料庫中的內容~所以語句中要寫清是哪個資料庫中的表,所以這步不需要使用use來切換資料庫,隨便在哪個資料庫裡面都可以

看清了嗎?這句就是

select test.pet.*,huihui.user.name from test.pet,huihui.user where test.pet.id=huihui.user.id;

4、發現了吧,只要把位址寫清楚,mysql就可以乖乖的找到你想要查詢的資料庫和表了。

所以,如果是查詢不同機器上的資料庫,只需要把機器的位址寫對就可以了,

也就是把上個例子中的「test.pet」寫成「ip.test.pet」

條件所限,例子等我用過了再寫吧~嘿嘿

mysql不同資料庫不同資料表匯入資料

背景 現在我有這麼乙個需求 資料庫a的user表需要匯入到資料庫b的account表 user表字段 uid,username,e account表字段 id,name,email,password,type,salt 匯入的字段只有username,email,p並且regdate需要符合某個條件...

SQLserver不同資料庫不同表之間的複製

1.將eems庫中的dec towninfo表中的資料 複製到oem庫的c towninfo中 c towninfo表事先存在 先要將表的主鍵設為自增長型別,且列出列名 set identity insert oem dbo c towninfo oninsert into oem dbo c to...

MySQL不同資料庫之間表的簡單同步

mysql不同資料庫之間表的簡單同步,實用輕量級資料 如下案列展示 例如我現在主庫上面有users tenants兩張表需要同步到備庫上面 主庫 1 確認主庫資料條數 select count from users select count from tenants 2 將資料匯出到檔案,data ...