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
Related
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.
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
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 need to find rows that contain a specific number in a set of numeric values that are stored in a table. I'm using the WHERE IN() function of mysql, but I'm having problems with the proper format.
Basically I have the following query:
SELECT id,category, text
FROM ws_cat
WHERE '11' IN (category)
The category field is a VARCHAR and looks like the following:
id category
1 11
2 12,11
3 1,13,9
So I need to find the rows with id 1 and 2 in this case. Unfortunately it doesn't work and I'm guessing it's because of the missing quotes, but all the ideas of reformating with QUOTES() or just changing the format of category to something like '12','11' wouldn't work either. Both would be possible for me as long as it works...
Use the FIND_IN_SET function:
SELECT id, category, text
FROM ws_cat
WHERE FIND_IN_SET('11', category) <> 0;
I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)