This query return users from database MariaDB (MySql).
I need to remove rows from result, where in surname is added "(old)" OR "- R".
How to edit a query?
SELECT au.id,
au.name,
au.surname
FROM statements s
JOIN aduser au ON au.id=s.id_usp
WHERE s.id_utv = 10
GROUP BY au.surname
ORDER BY au.surname ASC
From this result of query:
id | name | surname |
---|---|---|
124 | Mike | Argle |
221 | Mike | Argle (old) |
138 | Lisa | Doe |
126 | Lisa | Doe (old) |
123 | John | Harris |
135 | John | Harris – R |
324 | Ann | Perez |
329 | Tiffani | Perez |
To this result. Query should return this result:
id | name | surname |
---|---|---|
124 | Mike | Argle |
138 | Lisa | Doe |
123 | John | Harris |
324 | Ann | Perez |
329 | Tiffani | Perez |
2
Answers
Change
WHERE s.id_utv = 10
To
WHERE s.id_utv = 10 AND INSTR(au.surename,'(old’) = 0
You can use a
ROW_NUMBER
window function to select the first value among thename + ' - R'/' (old)'
doubled values.Check the demo here.
Your query would get updated as such: