I’m not the greatest SQL guy in the world and this problem has been driving me crazy for about a week now.
I have 3 tables. The first is an attributes table with the following data:
attr_group_table
attr_id group price prefix id
4 2 20.00 + 5000
1 2 30.00 + 5000
attr_table
attr_id id value_id price prefix
1 5000 2 10.00 -
4 5000 3 10.00 +
13 5000 9 50.00 +
name_table
id name
2 widget1
3 widget2
9 widget3
So basically, I need all the all the items from the attr_table that match the ID of 5000. HOWEVER, if those items exist in the attr_group_table with group=2 then use the price and prefix from the attr_group_table. Then just suck the names about of the name table. The only data I need returned is the attr_id, name, price, and prefix.
I’ve been messing around with joins, unions, if’s and just can’t get the data I want. Any help/suggestions would be great.
Use function COALESCE:
It will return the first non-null value of its arguments.