I'm working on a webapp with a set of data that users can browse and edit.
Users want to customize one of the fields that appear for each item, say "product_name".
Each product has a set of default values in a table, which is the same for all users:
The product table would be like (skipping syntax cruft, you get the idea):
CREATE TABLE products (
pid int, # product id
pname varchar # product name (users want to customize this)
);
I need to display the user's customized product name in all tables/lists on my site whenever there is a customized value for that product, but otherwise display the default value.
For space efficiency I thought of using an extra table that sort of "overrides" the default values, for any given user, for any given product. The rows are added in this table as needed. A user that did not customize any product name would not have any rows in there:
CREATE TABLE custom (
pid int,
userid int,
pname varchar
);
I can easily join these two tables on product id, and could pick the default (products.pname) , or user-edited value (custom.pname) for each row in php, but I'm hoping there is a more efficient way to do this in MySQL.
Is there an expression that lets me pick one column if not null otherwise pick another (accross two tables) ?
Would you suggest a different way to handle this ?
Thank you.
PS: maybe a type of JOIN that would allow a column of table 2 with the same name to override a column in table 1 for those rows where it exists in table 2 ?