Distinct values in set - mysql

the problem is I don't know how to write a query that does what I want. I hope you can help. I have a table like this:
path (VARCHAR) | info (VARCHAR) | method (SET)
------------------------------------------------------------
/ | something | GET
/cp/product/% | something else | GET,PUT
/cp/product/edit/% | yap yap | DELETE,POST
What I want is a query which would show all methods available for the path, e.g. if I were to run something like this:
SELECT distinct_values_in_set(`method`) FROM `table` WHERE '/cp/product/edit/1337' LIKE `vpath`;
I'd expect a result set similar to
distinct_values_in_set(`method`)
--------------------------------
DELETE,GET,POST,PUT

SELECT group_concat(distinct `method`)
FROM `table`
WHERE ...;

Related

MYSQL Like Improper results

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.

MySQL query - how to look for certain string in the field

I have a table "story" as follows:
+++++++++++++++++++++++++++++++++++++++++++
| id | keywords |
+++++++++++++++++++++++++++++++++++++++++++
| 1 | romance,movie,drama |
| 2 | newmovie,horor,comedy |
| 3 | movie,scifi |
| 4 | newmovie,romance,drama,asia |
| 5 | kids,movie |
+++++++++++++++++++++++++++++++++++++++++++
I try a query to search 'movie' in keywords field as below:
SELECT id FROM story WHERE keywords LIKE '%movie%'
and the result is
1,2,3,4,5
but in this case I wanted the result is 1,3,5 (field value with newmovie not include). Can someone help me how the query to do it?
Thank you for your help..
You want to use find_in_set like this:
SELECT id FROM story WHERE find_in_set('movie', keywords) > 0;
Though you should really consider normalizing your table structure.
In this case, you could've stored one single keyword in one row, then the query would be simply like:
select id from story where keyword = 'movie';
and that would've been the end of it. No heavy string functions needed.
You could have structure like this:
keywords(id, name);
story(story_id,. . ., keyword_id);
then, you could simply join the two like this:
select s.*
from story s
inner join keywords k on s.keyword_id = k.id
where k.name = 'movie';
Your problem is that "newmovie" can be found by "%movie%" you need only search "movie".

mysql - search for the exact word phrase without using regex nor rlike

I have my table like this:
|name |
-----------------
|Joseph Jackson |
|Aiden Chase |
|Luke Benjamin |
|Joseph |
|Joseph2 |
If I search for the word Joseph (SELECT * from myTable where name="Joseph" ) I woud get only the last result:
|name |
-----------------
|Joseph |
If I search for the word Jackson, I won't get any results.
That is what I DON'T want.
What I want is to get the records that exist in the exact word searched, for example:
if the word searched is Joseph, I woud get:
|name |
-----------------
|Joseph |
|Joseph Jackson |
*Notice that Joseph2 is not in the result set.
If the word searched is Jackson, I woud get:
|name |
-----------------
|Joseph Jackson |
*But I cannot use like '%Jose%' (the use of like) cuz I will get Joseph2 and I want the exact word(Jose).
*If the word searched is Jose for example I shouldn't get any result, cuz Jose is not in the data (also I shouldn't get records that contain the Joseph word).
The only way I've found is using regex for example:
SELECT * from myTable where REGEXP [[:<:]]Joseph[[:>:]]
|name |
-----------------
|Joseph |
|Joseph Jackson |
SELECT * from myTable where REGEXP [[:<:]]Jackson[[:>:]]
|name |
-----------------
|Joseph Jackson |
I want this results but without using regex and whithout rlike, is there any way? How? thanks.
This screams for FULLTEXT search
ALTER TABLE T ADD FULLTEXT INDEX (name);
SELECT * FROM T WHERE MATCH (name) AGAINST ('+Joseph' IN BOOLEAN MODE);
FIDDLE
Although full text as #Mihai suggests is definitely the way to go for production, if you want to do a one-off search without building extra indices you can just use LIKE by adding spaces around the names and search for the name including surrounding spaces;
SELECT * FROM myTable
WHERE CONCAT(' ', name, ' ') LIKE CONCAT('% ', 'joseph', ' %')
Note that this will definitely not use any indexes, so can be useful for admin purposes but not for a production application that cannot handle full table scans.
allow your stored procedure to accept a parameter for #name then you can use the WHERE clause
WHERE name = #name

How to use LIKE clause to compare two columns in the same 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, '%')")

SELECTing (almost) duplicate rows

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.