简体   繁体   中英

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

I have two table like this

  • sale (item_id, seller_id, price);
  • seller (id, name, address);

Some records in sale table may contain records that seller_id is not existed in the seller table.

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

  • 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.

  • simply replace target_column with 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

Alright so I think this shouldn't be too bad. You can get the behavior described in the third table using a LEFT JOIN . Here's an example of 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.

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.

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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