简体   繁体   中英

Get record based on priority from 2 table in MYSQL with single query

There 2 tables with same field

Tables

  1. product_settings
  2. store_product_settings

Field List

  1. product_setting_id

  2. products_id

  3. setting_constant_name

  4. setting_value

how to get record first from "store_product_settings" and if not found in "store_product_settings" then fetch from "product_settings" table

Note: Without Union Query

You want to use a left outer join on the tables, and then if the value in store_product_settings.setting_constant_name is null, use the value(s) from product_settings.

For my example, I'm assuming the following: 1) product_setting_id is a sequential ID for each table and are unrelated 2) products_id and setting_constant_name are the two key fields being used in the search (as in "I want setting X for product Y") 3) setting_value is the value you want the store to override the global product

So I set up the following values in the tables:

insert into product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (1,1,'S1','V1');
insert into product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (2,1,'S2','V2');
insert into product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (3,2,'S1','V1');
insert into product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (4,2,'S2','V2');
insert into product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (5,2,'S3','V3');

insert into store_product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (1,1,'S2','V2A');
insert into store_product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (2,2,'S1','V1A');
insert into store_product_settings (product_setting_id, products_id, setting_constant_name, setting_value) values (3,2,'S3','V3A');

First, a simple left outer join to show how everything would get returned:

select ps.*, sps.* 
from product_settings ps
left outer join store_product_settings sps
  on ps.products_id = sps.products_id
    and ps.setting_constant_name = sps.setting_constant_name

product_setting_id  products_id  setting_constant_name  setting_value  product_setting_id1  products_id1  setting_constant_name1  setting_value1
        1                 1               S1                  V1
        2                 1               S2                  V2              1                   1                 S2                  V2
        3                 2               S1                  V1              2                   2                 S1                  V1
        4                 2               S2                  V2
        5                 2               S3                  V3              3                   2                 S3                  V3

And now, select only the column(s) we want using ifnull on the store-value to return the general value:

select ifnull(sps.setting_value,ps.setting_value) as setting_value
from product_settings ps
  left outer join store_product_settings sps on ps.products_id = sps.products_id
    and ps.setting_constant_name = sps.setting_constant_name

Gives you this:

setting_value
    V1
    V2A
    V1A
    V2
    V3A

Expand the return set for whatever additional fields you want to capture.

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