My table looks something like this:
| id (int) | sentence (varchar) |
I want to find all rows that are almost the same except for one particular word. Eg:
| 230 | test |
| 321 | test sth |
...
| 329 | is (sth) it?
| 923 | is it?
The word that can be different is sth in this case. Ideally I could use some sort of "array" with the list of words that can be different.
Is this something I could do purely in SQL?
Just an untested quick shot, sorry, but I think you could do something like
SELECT * FROM table GROUP BY REPLACE(text, 'sth', '')
You can use SOUNDEX. So with the examples that you gave, these queries:
SELECT SOUNDEX('test')
SELECT SOUNDEX('test sth')
SELECT SOUNDEX('is (sth) it?')
SELECT SOUNDEX('is it?')
return these results:
T230
T230
I200
I200
That means that the first two and the second two sound like each other. What I can't be sure of is how well this will work with your actual data, you're just going to have to try it.
Related
I am using Like to find something pattern from table
sample table:
id | title |
=============
1 | f550 |
-------------
2 | f550 |
-------------
3 | f-550 |
-------------
4 | f 550 |
I am using LIke query to check my receords, so lets say if I search for f550 it is bringing only 2 records that is correct technically but I want all records having any pattern such as (f550,f-550,f 550)
Is there any way besides REGEX I can do?
My query fires like this
SELECT * FROM `qd_posts` WHERE title LIKE '%f550%';
I tried using different combinations such like this but didn't worked.
SELECT * FROM `qd_posts` WHERE title LIKE '%f%-%550%';
I have even tried using RLIKE but still not got the result
You could try using '%f%550%'
SELECT *
FROM `qd_posts`
WHERE title LIKE '%f%550%';
or
SELECT *
FROM `qd_posts`
WHERE title LIKE 'f%' AND LIKE '%550';
For the specific list given (f550,f-550,f 550), use this:
REGEXP '^f[- ]?550$'
Yes, LIKE 'f%550' matches the same things, but lots more.
Been struggling for this for awhile.
Is there a way to find all rows in my table where the word in the column 'word' is a part of a search word?
+---------+-----------------+
| id_word | word |
+---------+-----------------+
| 177041 | utvälj |
| 119270 | fonders |
| 39968 | flamländarens |
| 63567 | hänvisningarnas |
| 61244 | hovdansers |
+---------+-----------------+
I want to extract the row 119270, fonders. I want to do this by passing in the word 'plafonders'.
SELECT * FROM words WHERE word REGEXP 'plafonders$'
That query will of course not work in this case, would've been perfect if it had been the other way around.
Does anyone know a solution to this?
SELECT * FROM words WHERE 'plafonders' REGEXP concat(word, '$')
should accomplish what you want. Your regex:
plafonders$
is looking for plafonders at the end of the column. This is looking for everything the column has until its end, e.g. the regexp is fonders$ for 119270.
See https://regex101.com/r/Ytb3kg/1/ compared to https://regex101.com/r/Ytb3kg/2/.
MySQL's REGEXP does not handle accented letters very well. Perhaps it will work OK in your limited situation.
Here's a slightly faster approach (though it still requires a table scan):
SELECT * FROM words
WHERE 'PLAutvälj' =
RIGHT('PLAutvälj', CHAR_LENGTH(word)) = word;
(To check the accents, I picked a different word from your table.)
I need to verify if the value of Column_1 is contained in the values of Column_2. The type of data in the 2 columns are:
ID | column1 | column2 |
----------------------------
1 | apple | pear,grape, apple |
2 | pear | apple,grape |
3 | apple | apple |
The query should return to me the lines 1 and 3.
I've tried something like this, but doesn't work:
SELECT * FROM `table` as C WHERE column_1 LIKE "%C.column_2%"
Your comparison is backwards. It should be:
WHERE column2 LIKE CONCAT('%', column1, '%');
Note that this will return a row like:
5 apple pineapple,grapefruit
If that's not appropriate, you shouldn't use LIKE. FIND_IN_SET is designed to match items in a column-delimited list, so you could use:
WHERE FIND_IN_SET(column1, column2)
However, make sure you don't have any spaces around the commas if you do this.
It would be much better if you normalized your table. Comma-delimited lists should not be used, you should use a many-to-many relation table.
For those using eloquent, you could solve the problem as seen below:
table::whereRaw("column1 LIKE CONCAT('%', column2, '%')")
I have a table in which each column represent a text from a manuscript, here is a simple example:
mss1 | mss2 | mss3
------------------------
The | The | A
big | big | big
black | |
dog | dog | dog
I would like to display rows where two columns have different values (or the same values), for instance I want to see the differences between mss1 and mss3. The result should look like:
mss1 | mss3
---------------
The | A
black |
These seemed to be good solution candidates :
SELECT mss1, mss3 FROM table WHERE mss1 != mss3;
SELECT mss1, mss3 FROM table WHERE mss1 NOT LIKE mss3;
However it is not working, even after converting all columns from text to varchar of the same length.
I also tried LOCATE (See here) to find same values: if I can locate mss1 in mss3 and vice-versa, they must have the same value, right? But that was not successful either.
Any idea? It seems like it should be easy, but I can't figure it out...
Perhaps the issue with your query is that NULL values are filtered out. For your purposes, I think this may do what you want:
SELECT mss1, mss3
FROM table
WHERE coalesce(mss1, '') <> coalesce(mss3, '');
I'm trying create a MySQL query which will return a row (or more, but I'm only interested in a single row) an area suggestion when provided with a postcode.
So, I have a table like so
area_id | postcode_start
1 | NE
2 | DL
3 | DL1
4 | DL2
5 | DL6
...
And I wish to provide a postcode, for example DL5 8TB and for it to return the row 1 | DL as the first characters match.
Another example DL6 4GH would return two rows, 5 | DL6 and 2 | DL
SELECT *
FROM (`area_postcodes`)
WHERE CONCAT(LOWER(`area_postcodes`.`postcode_start`),'%')
LIKE 'dl5 8tb';
Apparently it's a valid query but it always returns an empty set.
You'll see I've added the wildcard % to the other side of the query but it doesn't seem to work and I don't know where else to even consider guessing how to go about it, it's almost like the reverse of a normal MySQL LIKE query.
You were very close:
SELECT
*
FROM
area_postcodes
WHERE
'dl5 8tb' LIKE CONCAT(postcode_start, '%');