sql横着连接起来sql_SQL联接

sql横着连接起来sql

Joins are a very powerful tool. Remember relational algebra from the database intro module?

联接是一个非常强大的工具。 还记得数据库介绍模块中的关系代数吗?

Joins are applied relational algebra.

连接是应用关系代数

Suppose you have 2 tables, people and cars:

假设您有2张桌子, people cars

CREATE TABLE people (
  age INT NOT NULL,
  name CHAR(20) NOT NULL PRIMARY KEY
);

CREATE TABLE cars (
  brand CHAR(20) NOT NULL,
  model CHAR(20) NOT NULL,
  owner CHAR(20) NOT NULL PRIMARY KEY
);

We add some data:

我们添加一些数据:

INSERT INTO people VALUES (37, 'Flavio');
INSERT INTO people VALUES (8, 'Roger');
INSERT INTO cars VALUES ('Ford', 'Fiesta', 'Flavio');
INSERT INTO cars VALUES ('Ford', 'Mustang', 'Roger');

Now say that we want to correlate the two tables, because the police stopped Roger driving, looks young, and want to know his age from their database.

现在,我们要关联这两个表,因为警察阻止了Roger的驾驶,看起来很年轻,并且想从他们的数据库中知道他的年龄。

Roger is my dog, but let’s suppose dogs can drive cars.

罗杰是我的狗,但让我们假设狗可以开车。

We can create a join with this syntax:

我们可以使用此语法创建一个连接

SELECT age FROM people JOIN cars ON people.name = cars.owner WHERE cars.model='Mustang';

We’ll get this result back:

我们将获得以下结果:

age 
-----
   8

What is happening? We are joining the two tables cars on two specific columns: name from the people table, and owner from the cars table.

怎么了? 我们将两个表格中的cars加入到两个特定的列中: people表中的namecars表中的owner

Joins are a topic that can grow in complexity because there are many different kind of joins that you can use to do fancier things with multiple tables, but here is the most basic example.

联接是一个可能会变得越来越复杂的主题,因为可以使用多种不同的联接对多个表进行更复杂的操作,但这是最基本的示例。

翻译自: https://flaviocopes.com/sql-joins/

sql横着连接起来sql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值