ZQueryEmpresas.Close;
ZQueryEmpresas.SQL.Clear;
filtro := 'WHERE ';
if palabras_claves.Text <> EmptyStr
then
filtro := filtro + '(palabras_claves like ' + QuotedStr('%' + palabras_claves.Text + '%') + ') and ';
if comunidad.KeyValue <> -1
then
if not ZTableComunidad.IsEmpty then
filtro := filtro + '(empresas.id_comunidad = ' + IntToStr(comunidad.KeyValue) + ') and ';
if provincia.KeyValue <> -1
then
if not ZTableProvincia.IsEmpty then
filtro := filtro + '(empresas.id_provincia = ' + IntToStr(provincia.KeyValue) + ') and ';
if comarca.KeyValue <> -1
then
if not ZTableComarca.IsEmpty then
filtro := filtro + '(empresas.id_comarca = ' + IntToStr(comarca.KeyValue) + ') and ';
if ayuntamiento.KeyValue <> -1
then
if not ZTableAyuntamiento.IsEmpty then
filtro := filtro + '(empresas.id_ayuntamiento = ' + IntToStr(ayuntamiento.KeyValue) + ') and ';
filtro := filtro + '(nombre LIKE ' + QuotedStr('%' + nombre.Text + '%') + ') or (nombre IS NULL)';
consulta := 'SELECT empresas.nombre, empresas.forma_juridica, empresas.palabras_claves, empresas.direccion, empresas.codigo_postal, ' +
'empresas.telefono_1, empresas.telefono_2, empresas.web, empresas.email_1, empresas.email_2, empresas.comentarios, empresas.id,
comunidad.comunidad, provincia.provincia, comarca.comarca, ayuntamiento.ayuntamiento ' +
'FROM (((empresas LEFT JOIN comunidad ON empresas.id_comunidad = comunidad.id) LEFT JOIN provincia ON
empresas.id_provincia = provincia.id) ' + 'LEFT JOIN comarca ON empresas.id_comarca = comarca.id) LEFT JOIN ayuntamiento ON empresas.id_ayuntamiento = ayuntamiento.id ' +
filtro +
' ORDER BY empresas.nombre, comunidad.comunidad, provincia.provincia, comarca.comarca, ayuntamiento.ayuntamiento';
ZQueryEmpresas.SQL.Add(consulta);
ZQueryEmpresas.Open;
ZQueryEmpresas.First;