Question

I've a poorly structured SQLite database. Structure looks like this:

ID | USERID | KEY | VALUE

The key column contains the name of a bit of data for a user, that means I have multiple rows with the same USERID. What I want to do is filter it so I get the following output:

Go through the entire database and search for the highest value that matches a certain key. For instance:

ID     |      USERID     |       KEY         |     VALUE
1      |      banana     |       abc12       |     3
2      |      apple      |       def34       |     4
3      |     pineapple   |       abc12       |     5

I only want to run the check if the word in the KEY column (e.g. abc12) is found. So if abc12 is the value of the column key it should check if it's value is higher then the last highest value. In the end, after running my query I should get the following result:

ID: 3 (since the value is 5, and it is the highest one out of abc12 keyword).

I'm looking forward for your ideas.

Was it helpful?

Solution

edit: whoa jk for some reason I thought you wanted the second highest value.

You can just use SELECT ID FROM [TABLE] WHERE KEY = [VALUE] ORDER BY VALUE DESC LIMIT 1

(old answer)

Something like SELECT VALUE FROM [TABLE] WHERE KEY LIKE "%:value:% (I'm assuming LIKE because you said it just wants contains, not a exact match) ORDER BY VALUE DESC LIMIT 1, 1 `

LIMIT 1,1 will select the second highest row (first row, offset 1). Not sure how this fails if there is only 1 row that matches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top