How do I pass an arbitrary number of strings to a query? - mysql

I am coming from a PostgreSQL background and there I could write the following:
SELECT * FROM my_table WHERE my_table.text_column = ANY(:input::text[])
In this query, I am passing an array of string values. This allowed me to pass any number of input parameters, without knowing the size in advance.
How would you do this in MySQL? I am targeting MySQL 8, if that matters.

One way is to use the IN keyword.
SELECT * FROM my_table WHERE my_table.text_column IN('item1','item2','item3');
http://www.mysqltutorial.org/sql-in.aspx
Edit: Without changing the query: If you create a temporary table for the list of items, you can use:
SELECT * FROM my_table WHERE my_table.text_column IN
(SELECT item from temptable);

Related

How to select a column WHERE its values are in json arrayin MySQL

In a word, I want to
select * from test.population where Number in (1,2,3),
but in the place of (1,2,3) I want to have a function that returns json array. So that I want to have this to be working like this.
select * from test.population where Number in ('[1,2,3]')
How to put json-array into where it clause?
You can use the MEMBER OF operator:
Number member of ('[1,2,3]')
You can use JSON_SEARCH(). It is available since MySQL 5.7, whereas MEMBER OF() came with MySQL 8.0:
select * from test.population where json_search('[1,2,3]', 'one', number) is not null

How to pass comma separated value in mysql query?

I am using a sub query to get all qcodes and passing it as a parameter to another query in mysql. But inner query is returning value like -
SELECT qcodes FROM boardmst WHERE id=10
10002','10028','10031','10202','10226
so how to parse it to pass in another query with IN clause?
SELECT * FROM users WHERE qcodes IN (SELECT qcodes FROM boardmst WHERE id=10)
Apart from the missing quotes on the outside (maybe it's not the exact answer the inner question produces) this is exactly what MySQL expects as the content of an IN clause. You just have to be sure that the definition of the qcodes column is identical between the users and the boardmst tables.
If you have only five values as comma separated then below query can come in handy :
SELECT * FROM users WHERE qcodes IN (
select regexp_substr(replace(qcodes,''',''',','),'[^, ]+',1,level)
from boardmst where id = 10 connect by level <= 5)

Is it possible to select data in mysql matching values from any of the existing columns?

Is there a way to select data from any of the existing columns in a databse?
Something like SELECT * FROM mytable WHERE AnyOfTheTableColumns = Something
No, column name can't be variable! You must define your query with explicit definition of columns as;
SELECT * FROM mytable
WHERE column1=Something OR column2=Something OR column3=Something

MySQL select all from list where value not in table

I cannot create a virtual table for this. Basically what I have, is a list of values:
'Succinylcholine','Thiamine','Trandate','Tridol Drip'
I want to know which of those values is not present in table1 and display them. Is this possible? I have tried using left joins and creating a variable with the list which I can compare to the table, but it returns the wrong results.
This is one of the things I have tried:
SET #list="'Amiodarone','Ammonia Inhalents','Aspirin';
SELECT #list FROM table1 where #list not in (
SELECT Description
FROM table1
);
With only narrow exceptions, you need to have data in table form to be able to obtain those data in your result set. This is the essential problem that all attempts at a solution to this problem run into, given that you cannot create a temporary table. If indeed you can provide the input in any form or format (per your comment), then you can provide it in the form of a subquery:
(
SELECT 'Amiodarone' AS description
UNION ALL
SELECT 'Ammonia Inhalents'
UNION ALL
SELECT 'Aspirin'
)
(Note that that exercises the biggest of the exceptions I noted: you can select scalars directly, without a base table. If you like, you can express that explicitly -- in MySQL and Oracle, at least -- by selecting FROM DUAL.)
In that case, this should work for you:
SELECT
a.description
FROM
(
SELECT 'Amiodarone' AS description
UNION ALL
SELECT 'Ammonia Inhalents'
UNION ALL
SELECT 'Aspirin'
) a
LEFT JOIN table1
ON a.description = table1.description
WHERE table1.description IS NULL
That won't work. the variable's contents will be treated as a monolithic string - one solid block of letters, not 3 separate comma-separated values. The query will be parsed/executed as:
SELECT ... WHERE "'Amio.....rin'" IN (x,y,z,...)
^--------------^--- string
Plus, since you're just doing a sub-select on the very same table, there's no point in this kind of a construct. You could try mysql find_in_set() function:
SELECT #list
FROM table1
WHERE FIND_IN_SET(Description, #list) <> ''

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).