请帮助我理解我的问题和最好的例子。
我无法理解如何创建对象并使用它们。
谢谢
答案 0 :(得分:0)
她的一些非常简单的东西:
准备DB:
CREATE TABLE `table1` (
`id` int(11) NOT NULL DEFAULT 0,
`value` text not null DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `table2` (
`id` int(11) NOT NULL DEFAULT 0,
`value` text not null DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO table1 VALUES
(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'), (5, 'five');
INSERT INTO table2 VALUES
(3, 'three'), (4, 'four'), (5, 'five'), (6, 'six'), (7, 'seven');
执行查询:
SELECT t1.*, t2.* FROM table1 t1 JOIN table2 t2 on t1.id = t2.id;
SELECT t1.*, t2.* FROM table1 t1 INNER JOIN table2 t2 on t1.id = t2.id;
+----+-------+----+-------+
| id | value | id | value |
+----+-------+----+-------+
| 3 | three | 3 | three |
| 4 | four | 4 | four |
| 5 | five | 5 | five |
+----+-------+----+-------+
SELECT t1.*, t2.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id = t2.id;
SELECT t1.*, t2.* FROM table1 t1 LEFT OUTER JOIN table2 t2 on t1.id = t2.id;
+----+-------+------+-------+
| id | value | id | value |
+----+-------+------+-------+
| 1 | one | NULL | NULL |
| 2 | two | NULL | NULL |
| 3 | three | 3 | three |
| 4 | four | 4 | four |
| 5 | five | 5 | five |
+----+-------+------+-------+
答案 1 :(得分:0)
假设你有两张桌子:
ID Name
1 Smith
2 Novak
3 Tarantino
和第二个表
ID Cash
2 300
3 490
4 500
INNER JOIN
将连接两个表中匹配的记录,即仅在结果表中ID
将包含2和3,因为它们匹配。
结果如下:
ID Name Cash
2 Novak 300
3 Tarantino 490
另一方面, OUTER JOIN
将匹配所有内容,无论它们是否匹配。如果记录没有匹配,则对应于某一行的值将为NULL
,即Smith NULL
列中的Cash
和Cash
列中的500对应记录将在NULL
列中显示Name
。
结果如下:
ID Name Cash
1 Smith NULL
2 Novak 300
3 Tarantino 490
4 NULL 500