In a table myTable
defined as:
+----+---------+-----------+
| id | name | value |
|----+---------+-----------+
| 7 | hand | right |
| 5 | hand | left |
| 0 | hand | both |
| 0 | feet | both |
| 0 | eyes | green |
| 9 | eyes | blue |
| 2 | eyes | white |
| 2 | hand | raised |
+----+---------+-----------+
Default settings are controlled by id = 0.
My question is how to write a select statement to get name,value for id = 5 in one query that will include set for id = 5 and any defaults not overridden.
The results should be:
+---------+-----------+
| name | value |
+---------+-----------+
| hand | left |
| feet | both |
| eyes | green |
+---------+-----------+
Should get you the right answer.
http://sqlfiddle.com/#!9/1f516/14
The following does not return three rows in the result set, it only returns a single row with columns
hand
,feet
andeyes
, so it may not work for you. But it should return the data that you're looking for given your conditions:Output:
This is approach generates 10 different
SELECT
statements rather than 1, and the accepted answer is, for most applications, probably a better way to go about it.It isn't clarified if the ordering of the result set is important, so might as well try:
Disclaimer: Tested in SQL Server, but not using anything specific to that version of SQL.
seems this one may work for you: