php - Multiple MySQL Queries in ONE While Ignoring Duplicate Matched IDs -


i asked question combine multiple mysql queries on same table one , received noteworthy answer @paul griffin.

as stated in other question linked above, have multiple queries combined 1 query. these queries consist of exact matching , broad matching search terms.

i searching through posts on wordpress website. keep things simple, i'm looking through following columns in posts table: post_title (article title), post_name (article url slug), , post_excerpt (article summary).

just keep things simpler sake of question, let's have search term of "floor finish", , i'm searching through column post_title.

as stated earlier, i'm looking exact matches , broad matches.

so 3 searches placed:

  1. floor finish
  2. floor
  3. finish

that produce query like:

( select `id`, `post_title` `tps_3_posts` `post_status` = 'publish' , `post_title` '%floor finish%' )   union   ( select `id`, `post_title` `tps_3_posts` `post_status` = 'publish' , `post_title` '%floor%' )   union   ( select `id`, `post_title` `tps_3_posts` `post_status` = 'publish' , `post_title` '%finish%' ) 

after examining query, began realize that's not efficient.

it's placing "exact match" ("floor finish") search first, followed each of "broad match" ("floor", "finish") searches.

so first go around, finds posts "floor finish" somewhere in title of article.

so there should list of id's selected.

now, when mysql goes place second search (the first "broad match" ("floor") search.) it's looking through posts find article title match containing "floor". includes looking through same posts have been located exact match (which don't want -- seems redundant.).

this same behavior continued each broad search. causing unnecessary searching.

so there way, multiple queries combined one, each following query doesn't through posts id (column), , post_title (column) has been selected?

or way of wording it... each query that's selecting same columns, has different search term, checks see if id (column) , post_title (column) has been selected, , if so, ignore id, , continue looking new id's haven't been selected.

or mysql handle internally since i'm issuing multiple queries combined 1 query?

edit

it seems on complicating all. using selected answer, , querying 3 columns mentioned towards top of question. (post_title, post_name, post_excerpt)

the query looks this:

(  select `id` , `post_name` , null `post_title` , null `post_excerpt` `tps_3_posts` `post_status` = 'publish'  , ( `post_name` '%floor-finish%' or  `post_name` '%floor%' or  `post_name` '%finish%' )  ) union (  select `id` , null `post_name` , `post_title` , null `post_excerpt` `tps_3_posts` `post_status` = 'publish'  , ( `post_title` '%floor finish%' or  `post_title` '%floor%' or  `post_title` '%finish%' )  ) union (  select `id` , null `post_name` , null `post_title` , `post_excerpt` `tps_3_posts` `post_status` = 'publish'  , ( `post_excerpt` '%floor finish%' or  `post_excerpt` '%floor%' or  `post_excerpt` '%finish%' )  ) 

try using or:

select `id`, `post_title` `tps_3_posts` `post_status` = 'publish' ,       (`post_title` '%floor finish%' or        `post_title` '%floor%' or        `post_title` '%finish%'       ); 

mysql not going parse like expression , determine 1 subset of another.

edit:

if want know how many of values match, include information in select:

select `id`, `post_title`,        ((`post_title` '%floor finish%') +         (`post_title` '%floor%') +         (`post_title` '%finish%')        ) nummatches           `tps_3_posts` `post_status` = 'publish' ,       (`post_title` '%floor finish%' or        `post_title` '%floor%' or        `post_title` '%finish%'       ); 

an exact match have value of 3.


Popular posts from this blog