I have a MySQL table setup where one column's values are a string of comma-separated True/False values (1s or 0s). For example, in the column, one field's value may be "0,1,0,0,0,0,1,1,0" and another may be "1,0,0,1,1,1,0,0,0" (note: these are NOT 9 separate columns, but a string in one column). I need to QUERY the MySQL table for elements that are "true"(1) for the "nth element" of that column's value/string.
So, if I was looking for rows, with a specific column, where the 3rd element of the column's value was 1, it would produce a list of results. So, in this case, I would only be searching for "1" in the fth place (12345 = X,X,X...) of the string (X,X,1,X,X,X,X,X,X,X). How can I query this?
This is a crude example of what I am trying to do ...
"SELECT tfcolumn FROM mytable WHERE substr({tfcolumn}, 0, 5)=1"
{tfcolumn} represents the column value
5 represents the 5th position of the string
=1 represents what I need that position to equal to.
Please help. Thanks
You can't. Once you put a serialized data type into a column in SQL (like comma separated lists, or JSON objects) you are preventing yourself from performing any query on the data in those columns. You have to pull the data in a different way and then use a program like python, VB, etc to get the comma separated values you are looking for.
Unless you want to deal with trying to make this mess of a query work...
I would recommend changing your table structure before it's too late. Although it is possible, it is not optimized in a format that a DBMS recognizes. Because of that the DBMS will spend a significant amount of time going through every record to parse the csv values which is something that it was not meant to be doing. Doing the query in SQL will take as much time (if not more time) than just pulling all the records and searching with a tool that can do it properly.
If the column contains values exactly like the ones you posted, then the Nth element is at the 2 * N - 1 position in the comma separated list.
So do this:
SELECT tfcolumn
FROM tablename
WHERE substr(tfcolumn, 2 * 5 - 1, 1) = '1'
Replace 5 with the index that you search for.
See the demo.
Or remove all commas and get the Nth char:
SELECT tfcolumn
FROM tablename
WHERE substr(replace(tfcolumn, ',', ''), 5, 1) = '1'
See the demo.
Try this
if substring_index(substring_index('0,1,0,0,0,0,1,1,0',',',3),',',-1)='1'
The first argument can be your column name. The second argument (',') tells the function that the string is comma-separated. The third argument takes the first 3 elements of the string. So, the output of inner substring_index is '0,1,0'.
The outer substring_index has -1 as the last argment. So, it starts counting in reverse direction & takes only 1 element starting from right.
For example, if the value in a particular row is '2,682,7003,14,185', then the value of substring_index(substring_index('2,682,7003,14,185',',',3),',',-1) is '7003'.
i am writing a query using full text search index is it right or wrong.
SELECT sum(p_bi.iQty)
FROM patientbillitem p_bi, patientbillpayment p_b
WHERE Match(p_bi.vItemCode) Against( 'pbi.vItemCode')
pbi.vItemCode is separate table column is take it as a string is it true or not?
As MySQL documentation on match() ... against() ... says:
AGAINST takes a string to search for, and an optional modifier that indicates what type of search to perform. The search string must be a string value that is constant during query evaluation. This rules out, for example, a table column because that can differ for each row.
This means that your query will search for the string literal of 'pbi.vItemCode' within p_bi.vItemCode field, not for the contents of the pbi.vItemCode field.
My MySQL table is not returning results with a MATCH (col) AGAINST ('') query.
The table is simple:
id | url | fullTextIndex
And my query is
SELECT *, Match(fullTextIndex) AGAINST ("7f7f7f807f8080807f8080807f7f7f807c828888808a86967e8b858d7f89838a76829e958f7badb68084a3a38384899077848b877f799f9c85799fa2827d8c8a ") FROM Pictures;
The last column, the match, is always 0. Except, I know for a fact that the string above is contained, verbatim, in one of the values.
Things to note:
The string is only in that row (so it is not in more than 50% of rows, so it shouldn't be ignored).
This is not the Full value
The column is a bigText column
When I use INSTR, I get the value 1 (which is correct)
Any ideas why this query might not be working?
There seems to be a (configurable) upper limitation on the length of the words considered for indexation:
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_ft_max_word_len
You can check the current value with SHOW VARIABLES LIKE "ft_max_word_len";
It returns 84 on my server, and your string is 128 chars long.
Suggested fix:
Add this line to your my.cnf file: ft_max_word_len=128 (or whatever max length you need)
Rebuild your indexes as advised on the MySQL website: REPAIR TABLE tbl_name QUICK;
I have a string array comprised of words (ex. { alpha, beta, gamma } ) and a MySQL table filled with words. For each string array, I come up with a SELECT statement that queries the MySQL table to see if there are any matches of words. The rows that are returned let me know when a word in my string array is one of the unique ones in the table. I then alter the text of the specific string in the string array. For simplicity sake, let's assume I want to call a .ToUpper() on it.
My current method is to get all of the rows from the table that match and then loop through the string array and check whether every single row returned matches every single string in the array. That's incredibly inefficient, and I would much rather have the rows returned from my MySQL query have a column that tells me which position in the string array that word came from. That way I can jump right there and fiddle around with the string. Is there a way to give each "LIKE" clause in the where statement a unique identifier that is returned if that specific like clause is the one that matches? Any ideas would be much appreciated.
My Select Statement is:
SELECT `WordText` FROM `Words` WHERE `WordText` LIKE 'alpha%' OR `WordText` LIKE 'gamma%';
What I am looking for is something like this:
SELECT `WordText`, PositionInArray FROM `Words` WHERE `WordText` LIKE 'alpha%' (IF MATCH THEN PositionInArray=0 -- alpha's position in my array) OR `WordText` LIKE 'gamma%' (IF MATCH THEN PositionInArray=2 - gamma's position in my array);
When this row is returned, I can go straight to WordArray[PositionInArray].ToUpper() and know that that is the word that matched.
Thanks a lot!
SELECT CASE WHEN WordText LIKE 'alpha%' THEN 0 WHEN WordText LIKE 'beta%' THEN 1
WHEN 'gamma%' THEN 2 ELSE -1 END AS match FROM Words WHERE match <> -1
I am trying to follow: http://dev.mysql.com/doc/refman/4.1/en/fulltext-natural-language.html
in an attempt to improve search queries, both in speed and the ability to order by score.
However when using this SQL ("skitt" is used as a search term just so I can try match Skittles).
SELECT
id,name,description,price,image,
MATCH (name,description)
AGAINST ('skitt')
AS score
FROM
products
WHERE
MATCH (name,description)
AGAINST ('skitt')
it returns 0 results. I am trying to find out why, I think I might have set my index's up wrong I'm not sure, this is the first time I've strayed away from LIKE!
Here is my table structure and data:
Thank you!
By default certain words are excluded from the search. These are called stopwords. "a" is an example of a stopword. You could test your query by using a word that is not a stopword, or you can disable stopwords:
How can I write full search index query which will not consider any stopwords?
If you want to also match prefixes use the truncation operator in boolean mode:
*
The asterisk serves as the truncation (or wildcard) operator. Unlike the other operators, it should be appended to the word to be affected. Words match if they begin with the word preceding the * operator.