MySQL WHERE LIKE by word with comma - mysql

How do I display result by SELECT ... WHERE LIKE with word field?
I've tried this SELECT * FROM tbl_question WHERE word LIKE '%How are you?%' but it doesn't work.
tbl_question
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 1 | How are you? | How |
| 2 | What's your name? | What, name |
| 3 | Hi there! | Hi |
| 4 | Hi, How are you? | Hi, How |
+-------------+-------------------+------------+
Example : If I search "How are you?", It will display results by 2 rows like :
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 1 | How are you? | How |
| 4 | Hi, How are you? | Hi, How |
+-------------+-------------------+------------+
or if I "Your name is?" it will display result like :
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 2 | What's your name? | What, name |
+-------------+-------------------+------------+

You can use regexp this way
select * from my_table where question REGEXP 'How|are|you|';

Try this :
SELECT * FROM tbl_question WHERE question LIKE '%How are you?%'

SELECT * FROM tbl_question WHERE word LIKE '%How are you?%'
By this your database will try to 'give you back' all the data that begin exactly with the sentence How are you? and as i see in the word you just have only specific words.
I created your table and inserted data into it and i use the query
SELECT
question FROM Custom WHERE word LIKE '%What%'
so it gave me back your second question.
If you can, explain to me what exactly you want to display.

You are taking wrong column name.please replace it with question.try this.SELECT * FROM tbl_question WHERE question LIKE %How are you%

Related

Mysql - BIGINT value is out of range in error using substring_index

select substring_index(SUBSTRING_INDEX(title, ' ', title+1), ' ',-1) as word ,
COUNT(*) AS counter
from feed_collections
group by word
ORDER BY counter DESC;
The table has 1785123 rows and I thing this is the problem.
This is the error query (1690): BIGINT value is out of range in '(feed_collections.title + 1)' and I don't know how to fix it.
The query worked until around 1500000 rows.
The table contains 3 columns: title(text), url(text), date(datetime).
The code is finding most common words in column title
Example:
Table
+----------------------------------+-----------------+
| title | url |
+----------------------------------+-----------------+
| the world of ukraine | www.ab |
| count the days until christmas | www.abc.com |
| EU and NATO wants to use bombs | www.abcd.com |
| Ukraine needs help from NATO | www.abce.com |
+----------------------------------+-----------------+
Result
+------+-------+
| word | total |
+------+-------+
| nato | 5 |
| of | 14 |
| and | 11 |
| To | 9 |
| that | 7 |
| ukraine | 2 |
| EU | 1 |
+------+-------+
I adapted the code from here:
How to find most popular word occurrences in MySQL?
This works with small data. Seems to be a problem when tries to filter large data.
What I'm trying to achive in the future is to find most common words in the title column grouped by 1,2,3,4,5,6,7 words.
It will exists a select box to select how many words to use.
Example:
I will select to find most common words with 4 words.
Title: 1. Nato is using force , 2. Eu and Nato is using force.
Results with 4 words:
'nato is using force' found 2 times in title.
Any idea how to fix or how to do a query for this?
I'm working with laravel, a solution would be to create a php method...

JSON extract multiple columns PostgreSQL

I had a question earlier: PostgreSQL trim text field with regex (or else) And I got a wonderful answer by a_horse_with_no_name. Now I have an additional question regarding this issue.
So here it is this rextester https://rextester.com/SUWG96428 and the goal is to have all the ids in a separate column. Is it possible at all?
Like this:
+---+----+-------+-------+
| | id | ids_1 | ids_2 |
+---+----+-------+-------+
| 1 | 1 | 4202 | 4203 |
| 2 | 2 | 4204 | |
| 3 | 3 | 4201 | |
+---+----+-------+-------+
Yep, you can modify your query like:
select
t.id,
right(((the_column::json->'itemID')->>0)::varchar, 4) as col1,
right(((the_column::json->'itemID')->>1)::varchar, 4) as col2,
right(((the_column::json->'itemID')->>2)::varchar, 4) as col3
from the_table t;
DB Fiddle

check if the value in any of these columns mysql

I got table called 'pet' have the following values
id | name | nickname | dateofborn |
1 | test | jhon | 2009 |
2 | test2| test | 2010 |
3 | mike | NULL | 2010 |
3 | jhon | testor | 2011 |
I want to select all of columns that contain 'test' value either in name column or nickname column so i have this query didn't actually work for me:
SELECT * FROM pet WHERE name='test' OR nickname= 'test'
note that I exactly want test value not testor.
The query is right, I think you have some problems with your Table.
But try like below:
USE [yourDB es: animals];
SELECT * FROM [yourDB].pet WHERE (name='test' OR nickname='test');
try this.
SELECT * FROM pet WHERE name like 'test%' OR nickname like 'test'

get first 3 alphanumeric characters (only numbers or letters)

I have a table which holds a field, title, I need to get first 3 alphanumeric characters of each title. Some of the values of title have ",',\t,\n, or whitespace prepended - this should be ignored.
+--------+-----------------------------------------+---------------------+
| id | title | desired output |
+--------+-----------------------------------------+---------------------+
| 1 | "abcd" | abc |
| 2 | 'lostworld | los |
| 3 | \tsonof | son |
| 4 | 12amrt | 12a |
+--------+-----------------------------------------+---------------------+
desired output is the output I am looking for. If anyone can suggest generic query which can handle all cases that would be great.
Looking for solution using MySQL only.
Your best bet is to use a regex user-defined function.
The built-in regexp functions only support matching; not string replacing like you want here

Check if a string contains numbers

How do I check if a string in a MySQL field contains a number then delete them?
Example table:
tbl_tags
-------------
| id | tag |
-------------
| 1 | hello |
| 2 | hello2 |
| 3 | 2hello |
| 4 | hel3lo |
-------------
The only way I can think of is to do each number individually and use LIKE '%1%' OR LIKE '%2%' etc. But there must be an easier way?
Check out the MySQL Regex methods. Something like the following should work.
SELECT * FROM table WHERE tag REGEXP '[0-9]'
SELECT *
FROM clients
WHERE name REGEXP '^[[:digit:]]+$'
ORDER BY `clients`.`country_id` DESC
LIMIT 0 , 30
this is the answer if you're looking for strings with only digits