I am using node.js with squel.js for SQL database.
I am storing array of string into one table field.
Let us say, My table name is like XYZ.
And my table data is like
id col2
1 '["a", "b", "x", "y"]'
2 '["x", "y", "q"]'
3 '["q", "e"]'
4 '["p", "q"]'
Now i want to know how to make query for search like
if i want to how to make query if i want to fetch records like
If i want to fetch all records which contains like ["x", "y"]. In this scenario i want expect result id 1 and 2.
If i want to fetch all records which contains like ["q"]. In this scenario i want expect result 2 and 4.
Please help me here i stuck here. Thanks in advance.
If I understand your question correctly,just use LIKE can do it
For the first requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"x"%` AND col2 LIKE `%"y"%`
OR
SELECT * FROM yourtable WHERE col2 LIKE `%"x","y"%`
Since I do not know the format exactly,is it just check match x and y? Or just match "x","y" exactly?
For the second requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"q"%`
col2 can be a JSON column, thus you can do something like:
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('x', 'y'));
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('q'));
The second query would returns rows 2, 3 and 4, but I guess your expectation might be incorrectly stated, since 'q' exists in the col2 array for all of those.
Related
i want some correction here. i want to select all people with name fred in database
Here's my query:
SELECT * FROM tdble WHERE CONCAT(name) LIKE CONCAT('%', REPLACE('fred', '')'%')
What you are asking can be simply achieved by either using the "=" operator of the wildcard operator "like" statement.
If you wish to find all records that have an exact match to the name 'Fred' then you should model your query as so:
Select * From tdble Where Name = 'fred'
However, if you want to get all results where the names have 'fred' included in it somewhere use the wildcard operator.
Select * From tdble Where Name like '%fred%'
Also you can further model your query to know where exactly in which form you want 'fred' to appear. Example if you want 'Fred' to be as the last characters of your name string, for instance you wish to get names which ends with fred then model your query like this:
Select * From tdble Where Name like '%fred'
(you will get results like 'alfred', provided there is an alfred in your table)
However if you wish to get all names that begin with fred, model the query like this:
Select * From tdble Where Name like 'fred%'
(you will get results like 'fredinane', provided there is a fredinane in your table)
Cheers
If you want to fetch record with name 'fred', you can simply do Select * from TableName Where Name = 'fred'.
If you want to fetch records which their names' string contain 'fred', you have to use select * from TableName where Name like '%fred%'
I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it
id text_1 text_2
1 おはよう おはよ
2 こんにちは ちわー
3 大丈夫 さよなら
4 でんわしたい でんわしよう
I have DB same above.
I want to search with input: おはよう大丈夫?
Expect result will match: id = 1 and id = 3.
Please help me how to query search in Mysql? Thanks you.
The following SQL query will fetch your 2 ids for exact string matching
select id from TABLENAME where text_1 in ('おはよう','大丈夫');
You can also use like operator with % to fetch approximately strings id.
You might face encoding issue (文字化け)depending on your DB settings, so you might have to convert SJIS <-> UTF-8
https://dev.mysql.com/doc/refman/5.7/en/faqs-cjk.html#faq-cjk-what-cjk-avail
Last but not least, if you want to use the full string as a comparison criteria to select the rows, then you can reuse the following code:
how to compute similarity between two strings in MYSQL
select id from TABLENAME where text_1 REGEXP "おはよう大丈夫"
I have a database (Mysql 5.6) like this :
id | value
--------------------------
1 | "value1;value2;value3"
2 | "value4;value5;value6"
I'd like to make a request like :
SELECT id FROM table WHERE 'value5' IS IN value
return --> 2
SQLFiddle : http://sqlfiddle.com/#!9/b5c347
I cannot change the database schema
Use LIKE syntax, but use it like this:
SELECT id
FROM table
WHERE
value LIKE 'value5;%' OR
value LIKE '%;value5' OR
value LIKE '%;value5;%' OR
value LIKE 'value5';
This is a dirty solution, and not possible to index, but should fit your needs (On a smallish table in a reasonable amount of time)
Another solution (If you're sure your values don't contain commas):
SELECT id
FROM table
WHERE
FIND_IN_SET('value5', REPLACE(value, ';', ',')
If all the values are different, you might be able to use LIKE:
SELECT id FROM table WHERE value LIKE '%value5%';
Just keep in mind that it won't work in case you have some value that contains the other, like
value2;value3;value56
Or use a RegExp, like this:
SELECT id FROM table WHERE value REGEXP '(^|[^a-z0-9])value5([^a-z0-9]|$)'
I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null