Hello,
I’m working on a searching system which requires lot of AND/OR conditions. Basically I want to search for posts into the database with Treatment and Location as search term. It’ll search for Clinic Locations in ‘_dac_clinic_location’ and Professionals Location in ‘_dac_location’ , also search for treatments in ‘_dac_profession’ field and ‘_dac_treatments_procedures’ field. I have the query below, but it’s not fetching anything. Can you please help on how to go about this query? Appreciate any help on this.
Thanks
$sql = "SELECT $meta_table.post_id, $post_table.post_status
From $meta_table, $post_table
WHERE $post_table.post_status <> 'hold'
AND $meta_table.post_id=$post_table.ID
AND(
(($meta_table.meta_key='_dac_profession' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_location' AND $meta_table.meta_value LIKE '%$location%'))
OR
(($meta_table.meta_key='_dac_treatments_procedures' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_location' AND $meta_table.meta_value LIKE '%$location%'))
OR
(($meta_table.meta_key='_dac_profession' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_additional_location' AND $meta_table.meta_value LIKE '%$location%'))
OR
(($meta_table.meta_key='_dac_treatments_procedures' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_additional_location' AND $meta_table.meta_value LIKE '%$location%'))
OR
(($meta_table.meta_key='_dac_profession' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_clinic_location' AND $meta_table.meta_value LIKE '%$location%'))
OR
(($meta_table.meta_key='_dac_treatments_procedures' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_clinic_location' AND $meta_table.meta_value LIKE '%$location%'))
)
LIMIT $offset, $rowsPerPage";
You’re looking for two different values in the same fields, and that’ll never occur. If you’ll have two separate records tied to the same record, then you’ll need to do an additional join
($meta_table.meta_key='_dac_profession' AND $meta_table.meta_value LIKE '%$treatment%')
AND ($meta_table.meta_key='_dac_location' AND $meta_table.meta_value LIKE '%$location%'))
SELECT $meta_table.post_id
, $post_table.post_status
FROM $post_table
INNER
JOIN $meta_table AS m1
ON m1.post_id = $post_table.ID
AND m1.meta_key IN ( '_dac_profession'
, '_dac_treatments_procedures')
AND m1.meta_value LIKE '%$treatment%')
INNER
JOIN $meta_table AS m2
ON m2.post_id = $post_table.ID
AND m2.meta_key IN ( '_dac_location'
, '_dac_additional_location'
, '_dac_clinic_location')
AND m2.meta_value LIKE '%$location%')
ORDER
BY something_so_LIMIT_makes_sense
LIMIT $offset
, $rowsPerPage
Thanks @r937 for your help on this.
I’m having another issue with this SQL. I want to order the result by the ‘dac_distance’ meta value. I’ve this sql for it now - But it’s not ordering the result as expected. Is there anything missing?
“SELECT DISTINCT m1.post_id, $post_table.post_status
FROM $post_table
INNER JOIN $meta_table AS m1
ON m1.post_id = $post_table.ID
AND m1.meta_key IN (‘_dac_profession’, ‘_dac_treatments_procedures’)
AND m1.meta_value LIKE ‘%$treatment%’
INNER JOIN $meta_table AS m2
ON m2.post_id = $post_table.ID
AND m2.meta_key IN (‘_dac_location’, ‘_dac_additional_location’, ‘_dac_clinic_location’)
AND m2.meta_value LIKE ‘%$location%’
INNER JOIN $meta_table AS m3
ON m3.post_id = $post_table.ID
AND m3.meta_key IN (‘_dac_distance’)
AND m3.meta_value <> ‘’
WHERE $post_table.post_status <> ‘hold’
ORDER BY m3.meta_value ASC
LIMIT $offset, $rowsPerPage”
On character type columns, sorting—like all other comparison operations—is normally performed in a case-insensitive fashion. This means that the order is undefined for columns that are identical except for their case. You can force a case-sensitive sort for a column by using BINARY like so: ORDER BY BINARY col_name.
The following table shows the resulting order of the values
if we use ORDER BY with different collations.
latin1_swedish_ci latin1_german1_ci latin1_german2_ci
Muffler Muffler Müller
MX Systems Müller Muffler
Müller MX Systems MX Systems