Below is my table, a User could have multiple profiles in certain languages, non-English profiles have a higher priority.
+----------+--------+----------------+----------------+ |ProfileID |UserID |ProfileLanguage |ProfilePriority | +----------+--------+----------------+----------------+ |1 |1 |en-US |2 | +----------+--------+----------------+----------------+ |2 |1 |es-MX |1 | +----------+--------+----------------+----------------+ |3 |1 |ja-JP |1 | +----------+--------+----------------+----------------+ |4 |2 |es-MX |1 | +----------+--------+----------------+----------------+ |5 |2 |ja-JP |2 | +----------+--------+----------------+----------------+ |6 |2 |de-DE |1 | +----------+--------+----------------+----------------+ |7 |3 |en-US |2 | +----------+--------+----------------+----------------+
For example: When a Spanish-speaking visitor requests my site (where ProfileLanguage = 'es-MX' or ProfilePriority = 2), I want the records like below:
+----------+--------+----------------+----------------+ |ProfileID |UserID |ProfileLanguage |ProfilePriority | +----------+--------+----------------+----------------+ |2 |1 |es-MX |1 | +----------+--------+----------------+----------------+ |5 |2 |ja-JP |2 | +----------+--------+----------------+----------------+ |7 |3 |en-US |2 | +----------+--------+----------------+----------------+
Below, is the basic SQL to get the users:
SELECT UserID, MIN(ProfilePriority) AS ProfilePriority
FROM Profile
WHERE ProfileLanguage = 'es-MX' OR ProfilePriority = 2
GROUP BY UserID
But as you know, I can only get the UserID, but I also need other column information, like ProfileID etc.. So I hope experts here could tell me the correct SQL expression to get the right records.