大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

mysql中怎么做关联 mysql内关联和左右关联的区别

mysql 中怎么关联表

第一:内联(inner join)

创新互联建站致力于网站建设,网站制作设计,营销网页按需搭建网站,外贸网站制作,企业网站建设,微信小程序,网站SEO优化,网站设计制作案例丰富,是成都做网站公司和建站公司,欢迎咨询。

如果想把用户信息、积分、等级都列出来,那么一般会这样写:

select * from T1, T3 where T1.userid = T3.userid

(其实这样的结果等同于select * from T1 inner join T3 on T1.userid=T3.userid )。

把两个表中都存在userid的行拼成一行(即内联),但后者的效率会比前者高很多,建议用后者(内联)的写法。

SQL语句:

select * from T1 inner join T2 on T1.userid = T2.userid

运行结果

T1.userid username password T2.userid jifen dengji

1 jack jackpwd 1 20 3

第二:左联(left outer join)

显示左表T1中的所有行,并把右表T2中符合条件加到左表T1中;

右表T2中不符合条件,就不用加入结果表中,并且NULL表示。

SQL语句:

select * from T1 left outer join T2 on T1.userid = T2.userid

运行结果

T1.userid username password T2.userid jifen dengji

1 jack jackpwd 1 20 3

2 owen owenpwd NULL NULL NULL

第三:右联(right outer join)。

显示右表T2中的所有行,并把左表T1中符合条件加到右表T2中;

左表T1中不符合条件,就不用加入结果表中,并且NULL表示。

SQL语句:

select * from T1 right outer join T2 on T1.userid = T2.userid

运行结果

T1.userid username password T2.userid jifen dengji

1 jack jackpwd 1 20 3

NULL NULL NULL 3 50 6

第四:全联(full outer join)

显示左表T1、右表T2两边中的所有行,即把左联结果表 + 右联结果表组合在一起,然后过滤掉重复的。

SQL语句:

select * from T1 full outer join T2 on T1.userid = T2.userid

mysql怎么让2个表关联起来

方法和操作步骤如下:

1、首先,创建一个测试表,如下图所示,然后进入下一步。

2、其次,插入测试数据,如下图所示,然后进入下一步。

3、接着,完成上述步骤后,查询表中的数据,“select t.* from test_tbl2 t ”,如下图所示,然后进入下一步。

4、最后,完成上述步骤后,编写sql,两个表通过pid与id关联, “select t1.*, t2.* from test_tbl1 t1 join test_tbl2 t2 on t1.p_id = t2.id;”,如下图所示。这样,问题就解决了。

mysql的简单的关联查询

1.新建表

create table websites(id int not null, name varchar(32), url varchar(100) not null ,alexa int not null , country varchar(32));

插入数据

insert into websites values(1,'Google',' ',1,'USA' );

insert into websites values(2,'taobao',' ',13,'CN' );

insert into websites values(3,'cainiao',' ',4689,'CN' );

insert into websites values(4,'weibo',' ',20,'CN' );

insert into websites values(5,'Facebook',' ',3,'USA' );

insert into websites values(7,'stackoverflow',' ',0,'IND' );

create table access_log(aid int not null, site_id int not null, count int, date varchar(100));

insert into access_log values(1,1,45,'2016-05-10');

insert into access_log values(2,3,100,'2016-05-13');

insert into access_log values(3,1,23,'2016-05-14');

insert into access_log values(4,2,10,'2016-05-14');

insert into access_log values(5,5,205,'2016-05-14');

insert into access_log values(6,4,13,'2016-05-15');

insert into access_log values(7,3,220,'2016-05-15');

insert into access_log values(8,5,545,'2016-05-16');

insert into access_log values(9,3,201,'2016-05-17');

insert into access_log values(10,6,111,'2016-03-19');

select * from mysql.test.websites

| 1 | Google | | 1 | USA |

| 2 | 淘宝 | | 13 | CN |

| 3 | 菜鸟教程 | | 4689 | CN |

| 4 | 微博 | | 20 | CN |

| 5 | Facebook | | 3 | USA |

| 7 | stackoverflow | | 0 | IND |

+-----+---------+-------+------------+

| aid | site_id | count | date |

+-----+---------+-------+------------+

| 1 | 1 | 45 | 2016-05-10 |

| 2 | 3 | 100 | 2016-05-13 |

| 3 | 1 | 230 | 2016-05-14 |

| 4 | 2 | 10 | 2016-05-14 |

| 5 | 5 | 205 | 2016-05-14 |

| 6 | 4 | 13 | 2016-05-15 |

| 7 | 3 | 220 | 2016-05-15 |

| 8 | 5 | 545 | 2016-05-16 |

| 9 | 3 | 201 | 2016-05-17 |

+-----+---------+-------+------------+

2.关联查询

内连接

SELECT *

FROM Websites

INNER JOIN access_log

ON Websites.id=access_log.site_id

ORDER BY Websites.id;

左连接

SELECT Websites.name, access_log.count, access_log.date

FROM Websites

LEFT JOIN access_log

ON Websites.id=access_log.site_id

ORDER BY access_log.count DESC;

右连接

SELECT Websites.name, access_log.count, access_log.date

FROM Websites

RIGHT JOIN access_log

ON Websites.id=access_log.site_id

ORDER BY access_log.count DESC;

全连接

SELECT websites.name, access_log.count, access_log.date

FROM websites

FULL OUTER JOIN access_log

ON access_log.site_id=websites.id

ORDER BY access_log.count DESC;

mysql 两个表中的信息怎么关联起来使用?

mysql

两个表中的信息关联起来使用方法:

1、创建主表:

create

table

UserInfo(

UserID

int

identity(1,1)

primary

key,

--递增主键

UserAccounts

varchar(20),

UserName

varchar(20),

UserPwd

varchar(10));

2、创建附表(含外键)

create

table

News(

NewsID

int

identity(1,1)

primarykey,

UserID

int,

NewsTitle

varchar(

50

),

NewsRelease

varchar(

200

),

NewsReleaseTime

datetime,

FOREIGN

KEY

(UserID)

REFERENCES

UserInfo(UserID));

--外键约束

如果附表已存在,但没外键,可采用以下方法:

alter

table

profession

add

constraint

fk_prov_id

foreign

key(prov_id)

references

province(prov_id)

on

update

cascade

on

delete

cascade;


当前文章:mysql中怎么做关联 mysql内关联和左右关联的区别
文章URL:http://dzwzjz.com/article/hgcess.html
在线咨询
服务热线
服务热线:028-86922220
TOP