How to use the LIKE operator with partial word matching? [duplicate] - mysql

SELECT * FROM store WHERE
concat_ws(name, type, location) LIKE :search1 OR :search2 OR :search3
for($n=0; $n<$count; $n++)
$query->bindValue(':search'.$n, '%'.$search[$n].'%', PDO::PARAM_STR);}
}
I have a search query, i break user's input into array, ex:array('i', 'love', 'apple');
my question is how to ORDER by the closest match?
it search 3 columns, so if user type new york apple, it will return many content with new york from location column, and many content are nothing to do with apple

If you want to do this, you will need Full Text Search(http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html). Which is a bit more complicated than LIKE, but certainly quicker and smarter.

You can do this without full text search (where the default values wouldn't work anyway for your query because "i" wouldn't be treated as a word):
order by ((concat_ws(name, type, location) LIKE :search1) +
(concat_ws(name, type, location) LIKE :search2) +
(concat_ws(name, type, location) LIKE :search3)
) desc
This orders by the most matches to the fewest. Your where clause needs to follow the same format as well, with three different like clauses.

Related

Transforming Results from Rows into Columns

I have a data set that contains both common and unique values, which I am attempting to return in a useable format to allow further analyse/work to be taken based on said results.
The desired result would be to have a script that would recognise the common values such as mpan/serial_number/read_at so as to only return a single row, but also to recognise the unique values those being the read_at and identifier.
Currently my script returns a unique row based on the identifier and the value, but I would like to be able to return a unique row for the read_at date for as many identifiers and values as are held. In most cases there are only two identifiers and values, but there could be as many as five.
The issue I have is that when I try to make distinct work, it will only then return the first found result, where I am expecting a pair of results at minimum. I am also unclear as to how I could stop getting a new row and instead create the result as an additional column?
My base script which pulls everything is as below, I have tried a few variances on this, but think this would likely be the best place to start from with regards to any help you may be able to offer?
SELECT *
FROM consumer.stg_d0010_v2_026_027
/*LEFT JOIN consumer.stg_d0010_v2_026_028_029
ON consumer.stg_d0010_v2_026_028_029.file_identifier = consumer.stg_d0010_v2_026_027.file_identifier
AND consumer.stg_d0010_v2_026_028_029.mpan = consumer.stg_d0010_v2_026_027.mpan*/
LEFT JOIN consumer.stg_d0010_v2_026_028_030_032
ON consumer.stg_d0010_v2_026_028_030_032.file_identifier = consumer.stg_d0010_v2_026_027.file_identifier
AND consumer.stg_d0010_v2_026_028_030_032.mpan = consumer.stg_d0010_v2_026_027.mpan
LEFT JOIN consumer.stg_d0010_v2_026_028_030_033
ON consumer.stg_d0010_v2_026_028_030_033.file_identifier = consumer.stg_d0010_v2_026_027.file_identifier
AND consumer.stg_d0010_v2_026_028_030_033.mpan = consumer.stg_d0010_v2_026_027.mpan
where consumer.stg_d0010_v2_026_028_030_032.read_At > '2022-10-01'
and consumer.stg_d0010_v2_026_027.mpan in (
)
Example dataset in image below.
enter image description here
And desired outcome
enter image description here
The issue I have is that when I try to make distinct work, it will only then return the first found result, where I am expecting a pair of results at minimum. I am also unclear as to how I could stop getting a new row and instead create the result as an additional column?

Searching ALL ROWS in a Group using IIF Expression

I am working on a report that displays patient names (as groups with drilldowns) and several fields related to their visits. I have created a column in the report to display whether or not a specific value appears in the 'LocationID' column. The expression I used is
=IIF(Fields!LocationID.Value="WELL","Y","N")
I thought this was working great, it displays Y or N next to each name to let me know if 'WELL' was in their 'LocationID'. I checked several to ensure that this was going to work and discovered that there was a LocationID code of 'WHS' and since I have the rows ordered by Name and LocationID if there was a WHS visit it shows up at the top of the group and my expression is only seeing this top item. How can this expression be written differently so that it searches the entire result of each group? Depending on the date range a patient may have one visit or they may have ten. I need to check all visits that are returned. Perhaps there is a better method. Thanks in advance.
I agree with jimmy8ball that the easiest way to solve most issues like this is to push some logic back into the SQL layer.
However, if you really want to do this via SSRS functionality, then you could implement a substring search against a lookupset. Assuming you have a patient id in your dataset that is unique for each patient (I hope your group isn't on the name) then...
=Iif(InStr(Join(Lookupset(Fields!patientid.Value, Fields!patientid.Value, Fields!LocationsID.Value, "dataset"), ","), "WELL") > 0, "Y", "N")
Which says, "Search through the dataset for all rows related to my patientid, join every location into a comma deliminated string, search the string for the text "WELL" and return "Y" if it's found.
Obviously if you have locations in your dataset like "WELLY", these will become false positives and you'll have to implement some more nested logic. Try appending a value (perhaps !) to the lookupset return field so that you can search for "WELL!" or some other terminator character.

Fuzzy Logic sorting

Starting off, I apologize for bad table structure, it was not my decision and existed before me.
Anyway, I have a table tbl_cities that is a list of, you guessed it, cities (but for whatever reason are stored in column [desc]). I wanted to make a nifty AJAX text input that, as you type, it tries to find out what cities you are typing and offer them as suggestions. It's kinda like this example. So my query looked like this
SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'
Which works fine. However, I see there are a bunch of misspellings in this table, so I want to add fuzzy logic. I want to keep getting cities that match the first letters they type, so I have this query.
SELECT [desc] FROM tbl_cities WHERE [desc] LIKE 'phil%'
OR DIFFERENCE('phil', [desc])>3
Now I want to sort based on the [desc] LIKE 'phil%' before the fuzzy logic part. So in this example, Philadelphia should appear before random cities like PAOLA and POWELL
I would try something like that :
order by case when [desc] like 'phil%' then 0 else 1 end, [desc]

return true or false in a mysql subselect if another field contains a string

I'm trying to return a value of true or false (or yes/no, etc.) as part of a select if a particular field in a table contains a substring.
Select the ID, name, and whether or not a person is subscribed.
A field called emailList has a comma separated list of names which should be used to check if the requester is subscribed. The result of the substring search should yield the true/false value as the result in another field.
The basic query would look like this:
SELECT
id,<...>,name
FROM
table
In the <...> area, I would want something equivalent to:
`emailList` contains #input ? "Yes" : "No"
I can't figure out how to do this to save my life. I'm guessing it can be done in other ways, but this seems like a good learning opportunity. Any suggestions?
Use IF():
SELECT
id,
IF(emailList LIKE '%string%', 'Yes', 'No') AS OnEmailList,
name
FROM
table
Just replace the word "string" with the search term you're looking for or a variable in your server-side programming language of choice.

Searching Mysql for similar string

People have different ideas of how to search for the same term.
For example Tri-Valley, Trivalley, Tri Valley (and possibly even incorrect spellings)
Currently that search is done like this
SELECT * FROM `table` WHERE `SchoolDistrict` LIKE '%tri valley%';
Is there an easy way to say 'space dash or no space' without writing out three like statements?
It seems like it could easily be done:
SELECT * FROM `table` WHERE `SchoolDistrict` LIKE '%tri%valley%';
But this only works if the initial input is 'tri-valley' or 'tri valley' If the initial input is 'trivalley' I have no idea where to place the % (theoretically that is, actually, I do, as we are only looking at about a dozen different school districts, but I'm looking to solve the larger problem)
You could consider using SOUNDEX, or SOUNDS LIKE if you have a lot of incorrect spellings. If you've got a lot of rows (or even if you don't), it might be wise to store the output of the SOUNDEX in an additional column.
I'd also recommend -- in the interests of accuracy -- introducing a separate table with an authoritative list of school districts, and run a query to find those which aren't in that list.
MySQL has a function called Sounds like.
link text
An alternative here is to recast the problem from search to select, if possible. Instead of letting your users enter free-form text to choose a school district, if you have a set of school districts generate a dropdown (or set of cascading dropdowns if the list is large, say by county, then by school district) and allow the user to select the appropriate one. Use this both for "searching" and for data entry to eliminate non-canonical entries. Obviously this only works when you can enumerate all of the entries.
Alternatively you could allow the user to choose a starts with or contains type search and simply generate the appropriate SQL ('tri%' or '%tri%') based on the selected search type. If the user understands that the search type is starts with or contains, they will likely adjust their search string until it yields the results they need.
The second statement you posted should do the trick:
SELECT * FROM 'table' WHERE 'SchoolDistrict' LIKE '%tri%valley%';
What you should do before you pass the search term into the select statement is to replace all characters and spaces with the % sign. For example,
SearchTerm = SearchTerm.Replace(" ","%");