从多个SQL表中选择数据
我有以下SQL表:从多个SQL表中选择数据,sql,Sql,我有以下SQL表: User\u表: -id_用户; -姓名; -(等) X_表: -id_x; -id_用户; -(等) Z_表: -id_Z; -id_用户; -(等) Y_表: -身份证; -id_用户; -(等) 我想从X、Z、Y表中选择所有数据,正确的语法是什么?有可能有两个以上的表吗?如果您想选择更多的表,您应该使用“join”从 例如: select U.id_user, X.id_x from User_table U join X_table X on U.id_user =
User\u表:
-id_用户;
-姓名;
-(等)代码>
X_表:
-id_x;
-id_用户;
-(等)代码>
Z_表:
-id_Z;
-id_用户;
-(等)代码>
Y_表:
-身份证;
-id_用户;
-(等)代码>
我想从X、Z、Y表中选择所有数据,正确的语法是什么?有可能有两个以上的表吗?如果您想选择更多的表,您应该使用“join”从
例如:
select U.id_user, X.id_x
from User_table U join X_table X on U.id_user = X.id_user
group by ...
第1部分-连接和联合
答案包括:
第一部分
使用内部联接联接两个或多个表(有关其他信息,请参阅wikipedia条目)
如何使用联合查询
左侧和右侧外部联接(此stackOverflow答案非常适合描述联接类型)
交叉查询(以及如果数据库不支持它们,如何复制它们)——这是SQL Server的函数(请参阅信息),也是我首先编写这一整件事的部分原因。
第二部分
子查询-它们是什么、在哪里可以使用以及需要注意什么
笛卡尔加入阿卡-哦,不幸!
有许多方法可以从数据库中的多个表中检索数据。在这个答案中,我将使用ANSI-92连接语法。这可能不同于其他一些使用较旧ANSI-89语法的教程(如果您习惯使用ANSI-89语法,可能看起来不那么直观,但我只能说尝试一下),因为当查询开始变得更复杂时,它更容易理解。为什么要用它?是否有性能提升?简短的回答是否定的,但一旦你习惯了,阅读起来就容易多了。使用这种语法阅读其他人编写的查询更容易
我还将使用一个小型停车场的概念,这个停车场有一个数据库来跟踪它有哪些可用的汽车。店主雇用你作为他的IT计算机人员,希望你能够立即向他提供他所要求的数据
我制作了许多查找表,最终的表将使用这些表。这将为我们提供一个合理的工作模型。首先,我将针对具有以下结构的示例数据库运行查询。我将试着思考开始时所犯的常见错误,并解释这些错误的原因——当然,也会展示如何纠正这些错误
第一张表只是一个颜色列表,这样我们就知道停车场里有什么颜色。
[预告]
[pre]
模型表将涵盖不同类型的汽车,使用不同的汽车类型比使用实际的汽车模型更简单
[预告]
[pre]
最后,把所有其他的桌子绑起来,把所有东西绑在一起的桌子。ID字段实际上是用于识别车辆的唯一批号
[预告]
[pre]
这将为我们提供足够的数据(我希望)来覆盖下面不同类型联接的示例,并提供足够的数据使它们有价值
因此,在这件事上,老板想知道他所有跑车的身份证
这是一个简单的两表联接。我们有一个表,用于标识模型和表中的可用库存。如您所见,cars表中模型列中的数据与cars表中的模型列相关。现在,我们知道models表的ID为1表示运动,所以让我们编写连接。
[预]
大家好,欢迎来到StackOverflow。在这里,我们试图帮助纠正代码,或者帮助人们提出即使通过示例也难以解决的问题。这是一个很容易解决的问题,但是,这不是一个免费的编码服务网站,所以我建议你阅读和阅读。希望这有帮助,祝你有一个愉快的一天,自己做家庭作业。(因为你的问题涉及到缺乏sql的基本知识,这是由于没有阅读基础知识而产生的,它会被否决)像这样`选择*从用户\表格内部联接X \表格联接Y \表格联接Z \表格,其中X \表格.id \表格用户=用户\表格.id \表格用户和Y \表格.id \表格用户=用户\表格.id \表格用户和Z \表格.id \表格用户=用户\表格.id \表格用户´
mysql> create table colors(id int(3) not null auto_increment primary key,
-> color varchar(15), paint varchar(10));
Query OK, 0 rows affected (0.01 sec)
mysql> show columns from colors;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| color | varchar(15) | YES | | NULL | |
| paint | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> insert into colors (color, paint) values ('Red', 'Metallic'),
-> ('Green', 'Gloss'), ('Blue', 'Metallic'),
-> ('White' 'Gloss'), ('Black' 'Gloss');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from colors;
+----+-------+----------+
| id | color | paint |
+----+-------+----------+
| 1 | Red | Metallic |
| 2 | Green | Gloss |
| 3 | Blue | Metallic |
| 4 | White | Gloss |
| 5 | Black | Gloss |
+----+-------+----------+
5 rows in set (0.00 sec)
The brands table identifies the different brands of the cars out caryard could possibly sell.
mysql> create table brands (id int(3) not null auto_increment primary key,
-> brand varchar(15));
Query OK, 0 rows affected (0.01 sec)
mysql> show columns from brands;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| brand | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> insert into brands (brand) values ('Ford'), ('Toyota'),
-> ('Nissan'), ('Smart'), ('BMW');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from brands;
+----+--------+
| id | brand |
+----+--------+
| 1 | Ford |
| 2 | Toyota |
| 3 | Nissan |
| 4 | Smart |
| 5 | BMW |
+----+--------+
5 rows in set (0.00 sec)
mysql> create table models (id int(3) not null auto_increment primary key,
-> model varchar(15));
Query OK, 0 rows affected (0.01 sec)
mysql> show columns from models;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| model | varchar(15) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into models (model) values ('Sports'), ('Sedan'), ('4WD'), ('Luxury');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from models;
+----+--------+
| id | model |
+----+--------+
| 1 | Sports |
| 2 | Sedan |
| 3 | 4WD |
| 4 | Luxury |
+----+--------+
4 rows in set (0.00 sec)
mysql> create table cars (id int(3) not null auto_increment primary key,
-> color int(3), brand int(3), model int(3));
Query OK, 0 rows affected (0.01 sec)
mysql> show columns from cars;
+-------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| color | int(3) | YES | | NULL | |
| brand | int(3) | YES | | NULL | |
| model | int(3) | YES | | NULL | |
+-------+--------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into cars (color, brand, model) values (1,2,1), (3,1,2), (5,3,1),
-> (4,4,2), (2,2,3), (3,5,4), (4,1,3), (2,2,1), (5,2,3), (4,5,1);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from cars;
+----+-------+-------+-------+
| id | color | brand | model |
+----+-------+-------+-------+
| 1 | 1 | 2 | 1 |
| 2 | 3 | 1 | 2 |
| 3 | 5 | 3 | 1 |
| 4 | 4 | 4 | 2 |
| 5 | 2 | 2 | 3 |
| 6 | 3 | 5 | 4 |
| 7 | 4 | 1 | 3 |
| 8 | 2 | 2 | 1 |
| 9 | 5 | 2 | 3 |
| 10 | 4 | 5 | 1 |
+----+-------+-------+-------+
10 rows in set (0.00 sec)
select
ID,
model
from
cars
join models
on model=ID