What should I use, to search for a keyword with mysql ?
I have a word and in the query I have
wordless
something word something
someword some
else
other
wordother
thingword
I want to output everything that has the word inside it, but the output to be like first outputed rows to be that rows with word as first letter on them, for example
wordless - will be the first because word are first characters of the word wordless
and the wordother to be outputed to to the first outputed rows, then after them to output something word something and etc, every word that contains the name word, but again to output first that rows that have the word at the first characters.
EDIT:
SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) asscoreFROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BYscoreDESC , Here i'm searching for d but it gives me an error -
Can't find FULLTEXT index matching the column list SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) as `score` FROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BY `score` DESC
Try to use Levenshtein algorithm in MySQL.
Levenshtein matching is a metric for measuring the amount of difference between two sequence, here it is strings. By default MySQL does not have this function, but you can write and add one.
Please take a look at the code here and add that code as a system function in MySQL, please see the example below on how to get the similarity of two strings.
Please see: https://github.com/rakesh-sankar/Tools/blob/master/MySQL/Levenshtein.txt
Example:
SELECT column1, LEVENSHTEIN(column1, 'matchme') AS perfectmatch FROM sometable ORDER BY perfectmatch DESC
Related
I am querying a MySQL table, and want it only to output items that match the word but can have other words as well. I am sort of looking for the equivalence of in MySql.
strpos ( string $haystack , string $needle)
PHP myAdmin TABLE Here is an image of my phpMyAdmin table. I am trying to search the table if the rightmost column, the archetype column. I am trying to find all results that match one word like Voltron, but the problem is that for example item #11, also has the word 'Keyword' in that column so it won't show.
Image I'm Searching By This is an example of what I want to search by. The last column, the archetype column is the variable. I want to query MySQL for all items that were Archetype like Tribal, or like Voltron, but can contain other 'Archetypes' as well.
When I query by this card, item number 3,9,10,11, and 15 should be outputted.
$aType = explode(" ", $aType);
$array_in_strings = join(", ",$aType);
//This Decides the Cards.
$query_all_items = mysqli_query($connect, "SELECT * FROM `edh__cardDatabase` WHERE `name` NOT LIKE '$name' AND (`archetype`) IN ('$array_in_strings') ORDER BY `name` ASC;");
And finally, this is what I have as the code.
Implode your words list to CSV string then use a query similar to
SELECT *
FROM {tablename}
WHERE FIND_IN_SET({columnname}, 'CSV,words,list')
AND {another conditions}
Pay attention - if you put a space between a comma and the next word then this space will be treated as a part of a word. I.e. 'word' will be found in 'this,is,a,word' but not found in 'this, is, a, word'.
I have field name "codehead" whose values are like
"53/066/10"
"54/066/05"
"56/066/09"
"52/069/15"
"53/069/02"
"67/069/02"
"00/020/80"
"00/020/98"
I want the results to be in following order
"00/020/80"
"00/020/98"
"53/066/05"
"53/066/10"
"54/066/09"
"52/069/15"
"53/069/02"
"67/069/02"
I have tried query like
$data= mysql_query("select codehead,sign, SUM(amt) as amt from pm where month='$pmmonth' and rc='R' GROUP BY substr(codehead,4,3) ASC,substr(codehead,7,2) ASC, sign ")
but could not get complete result.
This expression:
substr(codehead,7,2)
Is returning a forward slash and a digit. Looks like you wanted to start at position 8.
But that won't satisfy the order shown, the fifth row has 09, and that would come before 10 on the preceding row. The desired result shown makes it look like the result is also ordered on LEFT(codehead,2)
GROUP BY codehead, `sign`
ORDER BY substr(codehead,4,3) ASC
, LEFT(codehead,2) ASC
, substr(codehead,8,2) ASC
NOTE: In some future release of MySQL, the non-Standard MysQL extension of the GROUP BY to imply an ORDER BY may be removed.
I'm very suspicious of the GROUP BY on just the substrings. That's valid SQL to do that, I just question how you want to handle codehead values that match on the substrings, but is different in some other place... especially given that you're returning codehead in the SELECT list.
The normative pattern is to GROUP BY expressions in the SELECT list.
You can do something like this:
SELECT codehead,
substring(codehead,1,2) as 'one',
substring(codehead,4,3) as 'two',
substring(codehead,8,2) as 'three'
FROM foo
ORDER BY `two`, `one`, `three` ASC
SQLfiddle
and it will work, but it's going to have subpar performance because it's impossible to use indexes for that sort.
To address this you could add a new field to the table, eg sortkey, and insert a string in the format $two.$one.$three for each row. Then you can index and sort on that. Better yet, split all three values into their own fields and then you can index and sort however you want.
I'm building a search on my site and I noticed it doesn't work when you enter more than one word into the search. Here's the gist of the query:
SELECT * FROM `blog` WHERE `content` LIKE '%$keyword%' OR `title` LIKE '%$keyword%' ORDER BY `id` DESC
The weird things is that when I test the query in phpMyAdmin it returns the expected results. On my website however, no results are found.
I tried replacing spaces in the keyword with %s, but that didn't change anything.
The problem is that LIKE does pattern matching rather than actually search for keywords. You should create a fulltext Index on your database columns and use WHERE MATCH keywords AGAINST column. That will properly search for all keywords in any order and be a lot faster anyway.
I just tried this in my database and using LIKE in the query is more than 66 times as fast than using MATCH with fulltext index. I'm using two tables which are "connected" to each other. One is tags and the other one is products.
So what I did was that I added a fulltext index to the tag column in the tags table and performed the match against that column. The query than joins the products and then spits out some data about the item. That took about 4 seconds with ~3000 products & ~3000 tags.
I then tried it by first exploding the search string by whitespaces, and then imploding the result with %' OR tags.tag LIKE '%. This took about 0,06 seconds with the same amount of products and tags.
I have a field with this kind of info "web-1/1.,web-2/2.,web-3/3.,web-4/4.,web-5/5.". Other registers could have different values like "web-1/4.,web-2/5.,web-3/1.,web-4/2.,web-5/3."
I want to select and order by lets say web-2/? would be web-2/1, web-2/2, web-2/3 and so on all fields that contain web-2 and order by the last number
I want to create a featured properties script different websites and specify feature number. Different properties, different websites different order
I would suggest that you look at the MySQL String Functions and more specifically the SUBSTRING_INDEX function. The reason I suggest this one over SUBSTRING is because the number before or after the slash might be more than a single number which would make the length of the first and/or second parts vary.
Example:
SELECT `info`,
SUBSTRING_INDEX(`info`, '/', 1) AS `first_part`,
SUBSTRING_INDEX(`info`, '/', -1) AS `second_part`
FROM `table`
ORDER BY `first_part` ASC,
`second_part` ASC;
Result:
Additional Example
In this example, I'm using CAST to convert the second part into an unsigned integer just in case it contains additional characters such as symbols or letters. In other words, the second part of "web-4/15." would be "15" and the second part of "web-4/15****" would also be "15".
SELECT `info`,
SUBSTRING_INDEX(`info`, '/', 1) AS `first_part`,
CAST(SUBSTRING_INDEX(`info`, '/', -1) AS UNSIGNED) `second_part`
FROM `table`
ORDER BY `first_part` ASC,
`second_part` ASC;
If the strings will always match the pattern you described, we can assume that the first value you want to sort on is index position 5 in the string (5 characters from the left). The second character is index 7. With that in mind, you can do the following, assuming that the string 'web-2/1' is in a field named field:
SELECT
`field`
FROM
`table`
ORDER BY
substr(`field`, 5, 1) ASC,
substr(`field`, 7, 1) ASC;
The substr() functions take the field as the first option, the index we mentioned above, and an option third parameter which is the count for how many characters to include starting from the second option.
You can tweak this as necessary if the string is slightly off, the main thing being the second option in the subtr() function.
Just add: ...ORDER BY SUBSTRING(username, 2) ASC
I would do this
SELECT info
FROM table
WHERE info LIKE 'web-2%'
ORDER BY info ASC
I have the most simple SQL
SELECT * FROM words
It has 13000 words (varchar). I need to get the longest word first in my output. I guess it might be possible with the WHERE command?
Alternative
If it doesn't work like that, is there a smart way to sort my output array so it will order it by the longest word first (in the 'word'-column). It looks like this (in the output loop)?
$output_array[$row['word']] = $row['another_word'];
Ordering the words by its length should do it:
SELECT *
FROM words
ORDER BY LENGTH(word) DESC
select * from words order by len(my_field) desc;