我试图通过加入2个这样的表来创建一个MySQL视图:
用户表:
var sortedList = from p in marketItems
group p by p.ParentGroup into Groups
select Groups;
//to access the items in each group individually
foreach (var grouping in sortedList)
{
foreach (var Item in grouping)
{
Item.Description///you can access the individual items from this layer...
}
}
技能表:
+----+------+
| id | name |
+----+------+
| 1 | Joy |
+----+------+
| 2 | Roy |
+----+------+
| 3 | Tony |
+----+------+
技能表格中的+---------+---------+------------+
| auto_id | user_id | skill |
+---------+---------+------------+
| 1 | 1 | PHP |
+---------+---------+------------+
| 2 | 1 | CSS |
+---------+---------+------------+
| 3 | 2 | Ruby |
+---------+---------+------------+
| 4 | 2 | Javascript |
+---------+---------+------------+
是用户表格中user_id
的外键。
查询创建视图:
id
现在问题是创建的视图只有2行,我知道它因为 user_id 3 的技能表中没有值。现在我想要的是将 Tony' 技能的所有3行作为 Null 值,如下所示:
CREATE OR REPLACE VIEW user_view AS SELECT u.id, u.name, GROUP_CONCAT(s.skill) as
skills FROM users u, skills s WHERE u.id = s.user_id GROUP BY u.id
答案 0 :(得分:3)
您正在寻找Left join
:
CREATE OR REPLACE VIEW user_view AS
SELECT
u.id,
u.name,
GROUP_CONCAT(s.skill) AS skills
FROM
users u
LEFT JOIN
skills s ON u.id = s.user_id
GROUP BY u.id