简体   繁体   English

缺少外键时如何使用默认值 - mysql

[英]How to use default value when foreign key is missing - mysql

I have two table like this我有两张这样的桌子

  • sale (item_id, seller_id, price);销售(item_id,seller_id,价格);
  • seller (id, name, address);卖家(身份证、姓名、地址);

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 item_id seller_id卖家ID price价格
1 1 100 100 55 55
2 2 101 101 33 33
3 3 101 101 44 44

seller table:卖家表:

id ID name姓名 address地址
101 101 Ann 13 str 13 条
102 102 Ben 55 hs.xxx 55 hs.xxx
103 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 item_id seller_name卖家名称
1 1
2 2 Ann
3 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM