i have a column in my users database called "tags" which could look like this:
`[1,2,3,4]`
That array only has numbers.
I am now wondering if I can select from the database where the json array has specific value in it. For example:
`SELECT * FROM users WHERE tags = "HAS 1 IN ARRAY"`
I was testing by just matching the number 1 but then it will probably return values with 10 to 19 if any user has number 10-19 in that array.
Try this:
SELECT * FROM users WHERE tags REGEXP '[[:<:]]1[[:>:]]';
For more info, see docs Regular Expressions.
PS I would recommend to decompose table, move tags into dependent table. On large table regexp will be slow...
Related
I'm a little bit stuck.
I have an SQL column that contains weather codes (like Rain, Snow, etc.)
It is comma separated so the column would have a value of something like
rain,snow,haze
Now, I want to select the rows that contain values from an array.
I have an SQL code that is something like this:
SELECT * FROM locations WHERE currentWeather IN ('rain', 'snow', 'cloudy') ORDER BY name ASC
The problem is that this obviously works when currentWeather column only contains one item.
Is there a way to do it so that if the column value contains any of the items from the given array, it selects it?
Also, would it select it twice if two items match?
Best wishes
Use unnest in a subselect.
Select distinct A.myArray from (select unnest(column) as myArray from table) A where A.myArray in (your words to filter for)
Notice that using arrays in sql isn't very ideal and does not follows normalization rules. Your tables should ideally not contain arrays but rather just several rows each one containing the specific value you Want. It prevents issues such as this one.
To avoid the selection of repeated values, use the Distinct keyword right after you write select.
Rsference:
https://www.w3resource.com/PostgreSQL/postgresql_unnest-function.php
WHERE FIND_IN_SET(currentWeather, "rain,snow,cloudy")
Picks apart the string at commas (only) to see if currentWeather is any one of those 3 'words'.
See also FIELD(...)
I got table promos, with field store_id_list (VARCHAR). what i want to achieve here is i want this promo can be available into multiple store in 1 record, instead using multiple record, the store_id_list value is the list of store_id separated by comma (ex: 1,4,5,7,)
Now, i want to get record of table promo, where i have single store_id value, ex: store_id=5 how can i do that in MySQL? can i do that in single query? if using LIKE then more likely i can get 15 or 45 instead of 5.
My advice is to normalize your tables, and have a db-record per store/promo. But it can be done like this:
Make sure you have commas at the beginning and end of the column value, like this:
store_id_list : ,1,4,5,7,
And then query like this:
... where store_id_list like '%,5,%'
I think you are looking for FIND_IN_SET function. For example:
SELECT * FROM tbl WHERE FIND_IN_SET('5', store_id) > 0;
I stored array genres (action,adventure,animation) in mysql database.
If i want to retrieve all results of queries where that item have one of genre i searched for, for example animation, how could i do in php and mysql?
Please give me some instruction.
Thank anyway
It sounds like you're storing the genres as a comma-separated string. You can use the MySQL function FIND_IN_SET, i.e.:
SELECT *
FROM movies
WHERE FIND_IN_SET('adventure', genres) > 0
Another way to check a CSV field string is via the LIKE operator, like so:
SELECT *
FROM movies
WHERE ',' + genres + ',' LIKE '%,adventure,%'
We surround the genres field with commas so that the format is more regular.
Unfortunately, CSV fields don't index as easily, and your query may be slow. A better solution would be a many-to-many table of movies and genres.
I have a search form using a single text input with PHP, MySQLi.
Users search for names of people. In the MySQL table is a column 'name' and also a column 'nameNoSpaces' - this value is used when matching a user's search.
On input, the users query is stripped of all whitespace so that it also has no spaces. then I use the LIKE operator to compare the user's input (with no spaces) to the column 'nameNoSpaces'.
SELECT * FROM table WHERE nameNoSpaces LIKE '%query%'
If a value in 'nameNoSpaces' contains any part of the query string, it will be matched.
E.G. query 'hello you' will be matched to the value 'goodhelloyou'.
But not : query 'you hello'
I want to improve the search capabilities so that 'you hello' will return the row containing 'goodhelloyou'.
I have achieved this with PHP (foreach word do 'LIKE '%word%' and return all names that match every word).
BUT I was wondering if there is simply a SQL operator I can use to achieve this??
Thanks
SELECT * FROM table WHERE nameNoSpaces LIKE '%hello%you%'
Figured it out...should of seen it before
SELECT * FROM table WHERE name LIKE '%word1%' AND name LIKE '%word2%'....'%wordn%'
put all words into an array and use a foreach loop to create the query
I am having a table with a column that has few ids that were put into database with multi select. Column for example contains: 1,4,5,7,9. Is it possible to check if this column contains for example number 5 or not in it through MySQL query ?.
I need to select all the people that have number 5 or some other listed in that field and print them through php.
http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set
SELECT ...
WHERE FIND_IN_SET(5, list_column)
But understand that this search is bound to be very slow. It cannot use an index, and it will cause a full table-scan (reading every row in the table). As the table grows, the query will become unusably slow.
Please read my answer to Is storing a delimited list in a database column really that bad?
You can use #MikeChristensen's answer to be more standard. Another trick with standard SQL is this:
select * from TableName
where ',' || ids || ',' LIKE '%,5,%'
(in standard SQL, || is the string concatenation operator, but in MySQL, you have to SET SQL_MODE=PIPES_AS_CONCAT or SET SQL_MODE=ANSI to get that behavior.)
Another MySQL-specific solution is to use a special word-boundary regular expression, which will match either the comma punctuation or beginning/end of string:
select * from TableName
where ids RLIKE '[[:<:]]5[[:>:]]'
None of these solutions scale well; they all cause table-scans. Sorry I understand you cannot change the database design, but if your project next requires to make the query faster, you can tell them it's not possible without redesigning the table.
Perhaps:
select * from TableName
where ids = '5' -- only 5
or ids like '5,%' -- begins with 5
or ids like '%,5' -- ends with 5
or ids like '%,5,%' -- 5 in the middle somewhere
It probably won't be very fast on large amounts of data. I'd suggest normalizing these multi-selection values into a new table, where each selection is a single row with a link to TableName.
select * from your_table where concat(',',target_column,',') like '%,5,%'
you can write the sql query like this, for example you are looking for the number 5
select * from your_table_name where ids='5'
if you want to check the result with php just tell me i will write it for you :)