mysql - Exact match, or do partial match if no exact match, in a single query? -


I have to find a MySQL database under the following conditions:

1) If there is an exact match keyword Is found to return the number of rows and exact match results.

2) If no exact match is found, return the number of rows and results with partial matching.

For example, if the keyword was cake , and there were following rows in the table, then the search only required 1 and 5 rows.

  1 | Cake 2 | Pancake 3 | Cupcake 4 | Fruit cake 5 | Cake 6 Pi  

However, if only the following rows were present, then search should only have 2-4 rows.

  2 | Pancake 3 | Cupcake 4 | Fruit cake 6 Pie  

It is trivial to do this in two questions, such as:

  SELECT SQL_CALC_FOUND_ROWS field with table WHERE word = `cake` // PHP check For the number of rows, if 0, select another query, the word '% cake' 'like the word' cake shape 'from the table from the SQL_CALC_FOUND_ROWS field  

To cut the tour, is there any way to do this in a single question?

I can come up with the best solution or use ORDER BY to match exact through both. However, if an exact match is found to be deducted in partial matches, then it is necessary to rotate through the results, which is ineligible if exact match gives 2,000 results and partial matches gives more than 5000, but only the first 25 I want to show that while still showing 2,000 count.

edit 3:

works:

 Choose  COALESCE (NULLIF (COUNT (exact.ID), 0), COUNT (partial.ID)) test partial left to right on exact test. Id = partial.ID and exact.name = 'pie' where partial .name '% pi%'  

I deleted my last text here because it was wrong.

Edit 4:

The following is written in the TSQL (I do not know the correct mysqy syntax) and returns the actual rows (you wrap it in the stored proxyder on your MySQL server Can):

  create table # result (id et, name innovation (max)); INSERT #Result SELECT * FROM #YourCakeTable WHERE name = 'cake' if (FOUND_ROWS () = 0) # Select referral # Where to find your cacheable name like '% cc' * * # result [/ code> Pre> 

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -