'Creating resulting table from three tables which use id or uuid
I have one database mariadb 10.7 with name grocery. There are 3 tables
- grocery.goods
- grocery.shop
- grocery.suppliers
The tables use the primary key sometimes id, sometimes uuid. Old data use id, new data uuid. It's not my decision, it's set up in the company where I work. It is not possible to add new columns to the tables.
UUID example: "123e4567-e89b-12d3-a456-426614174000". In this question is used only part of uuid for example "ab".
Table grocery.goods:
┌───────┬─────────┬──────────┬──────────┬──────────────┐
│ id_gd │ uuid_gd │ id_sh │ uuid_sh │ from_country │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ 7 │ null │ null │ ab │ Brazil │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ null │ gn │ null │ ab │ Mexico │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ null │ gp │ 3 │ null │ Paraguay │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ 9 │ null │ 3 │ null │ Ecuador │
└───────┴─────────┴──────────┴──────────┴──────────────┘
Table grocery.shop:
┌───────┬─────────┬────────────┐
│ id_sh │ uuid_sh │ item │
├───────┼─────────┼────────────┤
│ null │ ab │ pineapples │
├───────┼─────────┼────────────┤
│ 3 │ null │ bananas │
└───────┴─────────┴────────────┘
Table grocery.suppliers:
┌───────┬─────────┬──────────┬──────────┬──────────────┐
│ id_sp │ uuid_sp │ id_sh │ uuid_sh │ supp_company │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ 12 │ null │ null │ ab │ company1 │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ null │ sd │ null │ ab │ company2 │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ null │ sr │ 3 │ null │ company3 │
├───────┼─────────┼──────────┼──────────┼──────────────┤
│ 13 │ null │ 3 │ null │ company4 │
└───────┴─────────┴──────────┴──────────┴──────────────┘
================================================================
Resulting table:
┌──────────┬──────────┬────────────┬──────────────┬────────────────┐
│ id_sh │ uuid_sh │ item │ from_country │ supp_company │
├──────────┼──────────┼────────────┼──────────────┼────────────────┤
│ null │ ab │ pineapples │ Brazil │ company1 │
├──────────┼──────────┼────────────┼──────────────┼────────────────┤
│ 3 │ null │ bananas │ Paraguay │ company3 │
└──────────┴──────────┴────────────┴──────────────┴────────────────┘
It is basically just a table grocery.shop with added columns "from_country" from table grocery.goods and "supp_company" from table grocery.suppliers.
From table grocery.goods is needed choose only first row with pineapples and first row with bananas.
From table grocery.suppliers is the same, is needed choose only first row with pineapples and first row with bananas. From table grocery.shop is needed to choose all rows.
I tried to create this resulting table with this query, but the query does not return the correct result.
SELECT DISTINCT shop.id_sh,
shop.uuid_sh,
shop.item,
goods.from_country,
suppliers.supp_company
FROM shop
LEFT JOIN goods
ON ((shop.id_sh IS NOT NULL AND shop.id_sh = goods.id_sh) OR
((shop.uuid_sh IS NOT NULL AND shop.uuid_sh = goods.uuid_sh))
LEFT JOIN suppliers
ON ((shop.id_sh IS NOT NULL AND suppliers.id_sh = shop.id_sh) OR
(shop.uuid_sh IS NOT NULL AND suppliers.uuid_sh = shop.uuid_sh))
Is it possible create this resulting table?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
