Forum Moderators: open
table name is ans_table
responseID ¦ questionID ¦ answer6 ¦ 4 ¦ abc
6 ¦ 5 ¦ cde
6 ¦ 6 ¦ tj@hotmail.com
8 ¦ 7 ¦ dkfjsdkf
8 ¦ 8 ¦ kjkd
8 ¦ 9 ¦ abc@hotmail.com
9 ¦ 7 ¦ dkd
9 ¦ 8 ¦ dkjds
9 ¦ 9 ¦ kdk@hotmail.com
10 ¦ 7 ¦ dkslsd
I need to select the lastest row with the email address, which in this case is the 2nd row from the bottom (kdk@hotmail.com). I don't know the email address, responseID, and questionID ahead of time, so the query cannot refer to a specific value in the table.
I only got:
select answer from ans_table where answer like "%@%" and .....
SELECT answer FROM ans_table WHERE responseID = (SELECT MAX(responseID) FROM ans_table WHERE answer LIKE "%@%") I'm assuming responseID's are assigned chronologically. I did do my tests on a table I have (just working out a similar situation), so I didn't test this verbatim; if it doesn't work, let me know and I'll be glad to have another go (if you haven't already figured it out).
There's also got to be a way using HAVING, but I wasn't able to figure it out, so while this might not be the most efficient, it's perhaps a start! Good luck.
responseID ¦ questionID ¦ answer
6 ¦ 4 ¦ abc
6 ¦ 5 ¦ cde
6 ¦ 6 ¦ tj@hotmail.com
8 ¦ 7 ¦ dkfjsdkf
8 ¦ 8 ¦ kjkd
1 ¦ 8 ¦ 9 ¦ abc@hotmail.com
9 ¦ 7 ¦ dkd
9 ¦ 8 ¦ dkjds
9 ¦ 9 ¦ kdk@hotmail.com
10 ¦ 7 ¦ dkslsd
from given result i don't think that respondID is as primary or unique key so what u can do just u add a auto increment value that will solve ur problem like :
sno ¦ responseID ¦ questionID ¦ answer
1 ¦ 6 ¦ 4 ¦ abc
2 ¦ 6 ¦ 5 ¦ cde
3 ¦ 6 ¦ 6 ¦ tj@hotmail.com
4 ¦ 8 ¦ 7 ¦ dkfjsdkf
5 ¦ 8 ¦ 8 ¦ kjkd
6 ¦ 8 ¦ 9 ¦ abc@hotmail.com
7 ¦ 9 ¦ 7 ¦ dkd
8 ¦ 9 ¦ 8 ¦ dkjds
9 ¦ 9 ¦ 9 ¦ kdk@hotmail.com
10 ¦ 10 ¦ 7 ¦ dkslsd
and u can use ,
SELECT answer FROM ans_table WHERE answer LIKE "%@%.%" ORDER BY sno DESC LIMIT 1;
6 ¦ 4 ¦ abc
6 ¦ 5 ¦ cde
6 ¦ 6 ¦ tj@hotmail.com
8 ¦ 7 ¦ dkfjsdkf
8 ¦ 8 ¦ kjkd
8 ¦ 9 ¦ abc@hotmail.com
9 ¦ 7 ¦ dkd
9 ¦ 8 ¦ dkjds
9 ¦ 9 ¦ kdk@hotmail.com
10 ¦ 7 ¦ dkslsd
10 ¦ 8 ¦ kjsdf@hotmail.com
Then your query would return both
dkslsd
kjsdf@hotmail.com