[英]How to use default value when foreign key is missing - mysql
I have two table like this我有两张这样的桌子
Some records in sale
table may contain records that seller_id is not existed in the seller
table. sale
表中的一些记录可能包含seller_id在seller
表中不存在的记录。
In example:例如:
sale table:销售表:
item_id ![]() |
seller_id![]() |
price![]() |
---|---|---|
1 ![]() |
100 ![]() |
55 ![]() |
2 ![]() |
101 ![]() |
33 ![]() |
3 ![]() |
101 ![]() |
44 ![]() |
seller table:卖家表:
id ![]() |
name![]() |
address![]() |
||
---|---|---|---|---|
101 ![]() |
Ann![]() |
13 str ![]() |
||
102 ![]() |
Ben![]() |
55 hs.xxx ![]() |
||
103 ![]() |
Anthony![]() |
no add![]() |
I want to query all sales with the seller's name.我想用卖家的名字查询所有的销售额。 If the seller has not existed, use the default name or empty.
如果卖家不存在,则使用默认名称或为空。 The result table for that query is something like this:
该查询的结果表如下所示:
item_id ![]() |
seller_name![]() |
---|---|
1 ![]() |
|
2 ![]() |
Ann![]() |
3 ![]() |
Ann![]() |
I tried to join tables with condition sale.seller_id = seller.id
, but it removed items that seller has not existed我尝试使用条件
sale.seller_id = seller.id
加入表格,但它删除了卖家不存在的项目
this can be achieved elegantly using COALESCE(val_1, val_2, val_3, ...val_n)
which is described as The COALESCE() function returns the first non-null value in a list.这可以使用
COALESCE(val_1, val_2, val_3, ...val_n)
优雅地实现,它被描述为COALESCE() function 返回列表中的第一个非空值。
simply replace target_column
with COALESCE(target_column, default_value)
只需将
target_column
替换为COALESCE(target_column, default_value)
Putting it all together, you can do something like.把它们放在一起,你可以做类似的事情。
select item_id, COALESCE(name, '') as seller_name from sale left join seller on sale.seller_id = seller.id;
Note: if you want to use some other value as default, replace empty single quotes with 'default name'注意:如果您想使用其他值作为默认值,请将空单引号替换为“默认名称”
read more here -> https://www.w3schools.com/mysql/func_mysql_coalesce.asp在这里阅读更多-> https://www.w3schools.com/mysql/func_mysql_coalesce.asp
Alright so I think this shouldn't be too bad.好吧,所以我认为这应该不会太糟糕。 You can get the behavior described in the third table using a
LEFT JOIN
.您可以使用
LEFT JOIN
获得第三个表中描述的行为。 Here's an example of LEFT JOIN
.这是
LEFT JOIN
的示例。
For your own notes, I did this using this online MySQL instance , but this should work with other instances of MySQL just fine.对于您自己的笔记,我使用此在线 MySQL 实例进行了此操作,但这应该可以与 MySQL 的其他实例一起使用。
I recreated the tables below, which I believe gives me what you have.我重新创建了下面的表格,我相信这给了我你所拥有的。 Since we're interested in the case where the
id
field and seller_id
field don't match, we could realistically add as many examples in here as you wanted as long as a few are missing a field or two in the Seller
table.由于我们对
id
字段和seller_id
字段不匹配的情况感兴趣,因此我们实际上可以在此处添加任意数量的示例,只要少数在Seller
表中缺少一两个字段即可。
CREATE TABLE Sale(item_id integer, seller_id integer, price integer);
INSERT INTO Sale(item_id, seller_id, price) VALUES
(1, 100, 55),
(2, 101, 33),
(3, 101, 44);
CREATE TABLE Seller(id integer, name varchar(20), addr varchar(20));
INSERT INTO Seller(id, name, addr) VALUES
(101, "Ann", "13 str"),
(102, "Ben", "55 hs.xxx"),
(103, "Anthony", "no add");
From here, you just do the left join to get the table you want.从这里,您只需执行左连接即可获得您想要的表格。 I also replaced the name variable with a
CASE
that replaces NULL
s with a name of your choice when a NULL is returned.我还用
CASE
替换了 name 变量,当返回 NULL 时,用您选择的名称替换NULL
。
SELECT Sale.item_id,
CASE WHEN Seller.name is NULL THEN "Default name" ELSE Seller.name END AS seller_name
FROM Sale
LEFT JOIN Seller
ON Sale.seller_id = Seller.id;
This returns to us a table containing the items of interest with their sellers, and as a result of the missing entry in Seller
, item_id=1
has a NULL field for its seller_id
.这将返回给我们一个包含卖家感兴趣的项目的表格,并且由于
Seller
中缺少条目, item_id=1
的 Seller_id 有一个seller_id
字段。
Edit because I don't have enough reputation to comment yet: Harsh's solution looks a lot cleaner, but I'll leave mine up in case its useful to you still.编辑因为我还没有足够的声誉来发表评论:Harsh 的解决方案看起来更干净,但我会留下我的,以防它对你仍然有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.