Necesito escribir una consulta sql que recupere y haga coincidir los registros de una tabla con las siguientes columnas;
first_name, second_name, attribute El objetivo es escribir una consulta, que coincida solo con aquellos registros donde el attribute de la columna tiene el siguiente formato;
<one or more arbitrary character>%<first name>_<second name>%<zero or more arbitrary characters> Cabe señalar que incluso las mayúsculas y minúsculas coinciden con first_name y second_name . La salida de muestra debería verse así;
first_name second_name attribute Vicenta Kravitz 0%Vicenta_Kravitz% Shayne Dahlquist 0R0V331K8Q7ypBi4Az3B6Nm0jCqUk%Shayne_Dahlquist%46E3O0u7t7 Mikel Kravitz PBX86iw1Ied87Z9OarE6sdSLdt%Mikel_Kravitz%W73XOY9YaOgi060r2x12D2EmDComo puede ver, los casos de las letras en first_name y last_name también coinciden. Aquí está mi intento;
SELECT first_name, second_name, attribute FROM table WHERE attribute REGEXP '^.+ CONCAT('%',binary(first_name),'_',binary(last_name),'%').*' ORDER BY attribute; Dado que la coincidencia de casos es un requisito, creo que la función binary() puede ayudar. Pero recibo el siguiente error de sintaxis;
ERROR 1064 (42000) at line 35: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_',binary(last_name),'%').*' ORDER BY attribute; END' at line 10Mirar el manual no está ayudando mucho. ¿Puedo obtener algunos comentarios sobre lo que puede estar fallando aquí? Gracias.
Tienes que concatenar la cadena de registro del agujero como
CREATE TABLE Table1 (`first_name` varchar(7), `second_name` varchar(9), `attribute` varchar(66)) ; INSERT INTO Table1 (`first_name`, `second_name`, `attribute`) VALUES ('Vicenta', 'Kravitz', '0%Vicenta_Kravitz%'), ('Shayne', 'Dahlquist', '0R0V331K8Q7ypBi4Az3B6Nm0jCqUk%Shayne_Dahlquist%46E3O0u7t7'), ('Mikel', 'Kravitz', 'PBX86iw1Ied87Z9OarE6sdSLdt%Mikel_Kravitz%W73XOY9YaOgi060r2x12D2EmD') ;
SELECT first_name, second_name, attribute FROM Table1 WHERE attribute REGEXP CONCAT('^.+%',binary(first_name),'_',binary(second_name),'%.*') ORDER BY attribute;nombre | segundo_nombre | atributo :--------- | :----------- | :------------------------------------------------- ---------------- Vicenta | Kravitz | 0%Vicenta_Kravitz% Shayne | Dahlquist | 0R0V331K8Q7ypBi4Az3B6Nm0jCqUk%Shayne_Dahlquist%46E3O0u7t7 Mikel | Kravitz | PBX86iw1Ied87Z9OarE6sdSLdt%Mikel_Kravitz%W73XOY9YaOgi060r2x12D2EmD
db<>violín aquí