Query MYSQL with WHERE clause predominance

Asked

Viewed 47 times

0

I have the following query (after all logic, ie the final query)

SELECT *
FROM cl_assistance AS CA
LEFT JOIN cl_assistance_rel AS CREL ON CA.cod_assistance = CREL.rel_assistance
WHERE latitude
BETWEEN -24.22070591
AND -22.88033389
AND longitude
BETWEEN - 47.30349541
AND - 45.96312339

What I need help with is that on the table cl_assistance_rel there is the foreign key rel_type which references in cl_assistance_type the type of assistance. Then there are 3 support options that comes in GET the request "Air-conditioners", "Stoves" and "Washers" (in code form).

So I need to include the query parameters:

CREL.rel_type = '1' OR
CREL.rel_type = '2' OR
CREL.rel_type = '3'

That is, first I take all posts within the given area, then I select the ones that fit the types of assists.

In case I join these two querys, I end up taking all the posts.

  • I’m not sure I understand, but try to add this at the end: AND CREL.rel_type IN (1,2,3)

1 answer

2


You need to separate query checks according to the logic you described:

first I pick up all posts within the given area, then I selects the ones that fit the types of assists

SELECT *
FROM cl_assistance AS CA
LEFT JOIN cl_assistance_rel AS CREL ON CA.cod_assistance = CREL.rel_assistance
WHERE 
(latitude
BETWEEN -24.22070591
AND -22.88033389)
AND 
(longitude
BETWEEN - 47.30349541
AND - 45.96312339)
AND
(CREL.rel_type = '1')

This query will take all assistances from a certain area that supports product 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.