mysql 在此SQL Join语句中等号是什么意思?

j7dteeu8  于 2023-06-28  发布在  Mysql
关注(0)|答案(4)|浏览(143)

我是sql的新手,不明白这个join语句在做什么。这个关于people.state_code=states.state_v的语句是否意味着people.state_code和states.state_v现在是一个?

SELECT people.first_name, 
       people.state_code, states.division
FROM people
JOIN states ON people.state_code=states.state_abbrev;
ljo96ir5

ljo96ir51#

它将从表people中取出列first_namestate_code,从表states中取出列division,并将它们放在一个连接表中,其中state_codestate_abbrev列中的条目匹配。连接表仅为响应此查询而显示而生成;不修改具有数据条目的基础表。

x8diyxa7

x8diyxa72#

在这种情况下,“=”表示相等(相似值相等),并且是连接条件的一部分,select语句根据该连接条件检索数据。您正在基于条件“链接”两个表,以便可以检索相关数据…
关系数据库-表之间和数据之间存在关系。
例如:

table_1
PERSON_ID FIRST_NAME LAST_NAME ADDRESS_ID
1        |John      |Doe      |2
table_2
ADRESS_ID STREET
1         | 5th Avenue
2         | 1st Street
SELECT FIRST_NAME, STREET 
FROM table_1 t1
JOIN table_2 t2 ON t1.ADDRESS_ID = t2.ADDRESS_ID;

会回来的

John, 1st Street
uemypmqf

uemypmqf3#

Does this statement ON people.state_code=states.state_abbrev mean that people.state_code and states.state_abbrev are now one?
回答:。people.state_code和states.state_v在各自的表上应该是相同的值。
让我给予一个来自https://www.mysqltutorial.org/mysql-join/的例子
假设你有下面的表格:

CREATE TABLE members (
    member_id INT AUTO_INCREMENT,
    members_name VARCHAR(100),
    PRIMARY KEY (member_id)
);

CREATE TABLE committees (
    committee_id INT AUTO_INCREMENT,
    committees_name VARCHAR(100),
    PRIMARY KEY (committee_id)
);

一些数据示例:

+-----------+--------+
| member_id | members_name |
+-----------+--------+
|         1 | John   |
|         2 | Jane   |
|         3 | Mary   |
|         4 | David  |
|         5 | Amelia |
+-----------+--------+

+--------------+--------+
| committee_id | committees_name |
+--------------+--------+
|            1 | John   |
|            2 | Mary   |
|            3 | Amelia |
|            4 | Joe    |
+--------------+--------+

要执行INNER JOIN,我们可以使用members_namecommittees_name,而不是id,因为它们是auto_increment,数据不相关。
所以查询应该是:

SELECT 
    m.member_id, 
    m.members_name  AS member, 
    c.committee_id, 
    c.committees_name  AS committee
FROM members m
INNER JOIN committees c ON c.name = m.name;

给出以下结果:

+-----------+--------+--------------+-----------+
| member_id | member | committee_id | committee |
+-----------+--------+--------------+-----------+
|         1 | John   |            1 | John      |
|         3 | Mary   |            2 | Mary      |
|         5 | Amelia |            3 | Amelia    |
+-----------+--------+--------------+-----------+

结论:各色谱柱测定值基本一致

8yoxcaq7

8yoxcaq74#

编写此查询的老派方法是:

SELECT people.first_name, 
       people.state_code, states.division
FROM people,
     states
WHERE people.state_code=states.state_abbrev;

使用WHERE子句是一种更清晰的方式来说明发生了什么。

相关问题