Reputation: 25
I have table with data in the format
+---+-------+----------+---------+----------+-------+----------+
|id | name1 | date1 | name2 | date2 | name3 | date3 |
+---+-------+----------+---------+----------+-------+----------+
| 1 | Jack | 02.03.13 | Michael | 07.12.10 | Clare | 08.15.99 |
+---+-------+----------+---------+----------+-------+----------+
The purpose of the Select query I am trying to make it to return the value of the "nameX" column if the "dateX" condition is met while X refers to same number.
Select nameX, dateX FROM tableName WHERE dateX = "08.15.99"
I am not sure how to make that X as variable as the date condition can fall at any column and it has to return its connected name column value
Upvotes: 1
Views: 1244
Reputation: 164089
Use a CASE
expression:
SELECT id,
CASE '08.15.99'
WHEN date1 THEN name1
WHEN date2 THEN name2
WHEN date3 THEN name3
END nameX,
'08.15.99' dateX
FROM tablename
WHERE '08.15.99' IN (date1, date2, date3);
See the demo.
Upvotes: 0
Reputation: 2539
Select Name1 AS Name,Date1 AS Date From tableName Where date1="08.15.99"
UNION ALL
Select Name2,Date2 From tableName Where date2="08.15.99"
UNION ALL
Select Name3,Date3 From tableName Where date3="08.15.99"
Your data source is unnormalized, making it awkward to deal with. If you have control over it you should rewrite it in normal form; one row per name-date pair.
Upvotes: 2