Select query to get particular pattern - mysql

I have a column which stores the data from the below format.
["12973","111","5555"].
I want to select the items which is not in the above set.
for ex,
If I search 12973 if it is exactly match it will not return.
When I use Like if I search "129" it also return the same result.
Any ideas to solve this problem..

If you are searching for an exact number you can use NOT LIKE to exclude the row
SELECT * FROM test WHERE col1 NOT LIKE '%"12973"%'
if you are searching for a row where no numbers start with 129 you can use REGEXP
SELECT * FROM test WHERE NOT col1 REGEXP '.*"129[0-9]*"'

What about search all results with LIKE and remove the exact matches?
SELECT * FROM t WHERE a LIKE '%129%' AND a != '129'

Related

Select row where id in array of mysql row

So I have an database where I have a string in each row eg: 1,4,56,3,23
I want to find every row where say 1 is in it, so this row would be found a 1 is at the start but if I have 4,1,54,32,2 it wont find it.
This is the code I'm using:
WHERE ".$id." IN ( outcomes )
You can use LIKE because you have a string separated by comma.
SELECT * FROM users WHERE outcome LIKE '41,%' OR outcome LIKE '%,41,%' OR outcome LIKE '%,41';
The above code select users which have outcome 41 in the list.
Use FIND_IN_SET :
SELECT *
FROM table
where FIND_IN_SET(".$id.",outcomes);
You can use the INSTR function as well :
SELECT *
FROM table
WHERE INSTR(".$id.",'1') > 0

SQL show results for A column first then show results for B column

I want SQL to show / order the results for the column name first then show results for the description column last.
Current SQL query:
SELECT * FROM products WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
I tried adding order by name, description [ASC|DESC] on the end but that didn't work.
It's for optimizing the search results. If a certain word is found in description it should go last if a certain word is also found in the name column.
You can use a CASE statement in an ORDER BY to prioritize name. In the example below all results where name is matched will come first because the CASE statement will evaluate to 1 whereas all other results will evaluate to 2.
I'm not sure by your problem description what exactly you want the behavior to be, but you can certainly use this technique to create more refined cases to prioritize your results.
SELECT *
FROM products
WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
ORDER BY CASE WHEN name LIKE '%$search_query%' THEN 1 ELSE 2 END
If you want the names first, the simplest order by is:
order by (name like '%$search_query%') desc
MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.
While this is undocumented, when results sets combined by a UNION ALL and not sorted afterwards, they stay in the order returned, as UNION ALL just adds new results to the bottom of the result set. This should work for you:
SELECT * FROM products
WHERE name LIKE '%$search_query%'
UNION ALL
SELECT * FROM products
WHERE (description LIKE '%$search_query%' AND name NOT LIKE '%$search_query%')

MySQL - WHERE x IN ( column)

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

MySQL - need to find records without a period in them

I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';

Mysql, how to order by the first number in a set of numbers

I need to order by a field that contains a set of numbers. Lets say a table named TEST contains ID, NAME, QUADS with QUADS as follows.
95,273,212,405
717,450,771,504
391,176,646,272
This are the results I am getting with a query such as
SELECT * FROM TEST ORDER BY QUADS
391,176,646,272
717,450,771,504
95,273,212,405
These are the results I am looking to get
95,273,212,405
391,176,646,272
717,450,771,504
I am only interested in the first number in the set for "order". Figure it might be possible with a substring to the comma but not sure how to do that in MySQL.
Try this:
select * from test
order by cast(substring_index(quads,',',1) as unsigned)
What you want is the substring_index function.
... order by substring_index(x_field,',',1)
This extracts the text in x_field up to the first occurrence of the comma delimiter
Try with this:
select QUADS, 0+QUADS as S from TEST order by S
0+QUADS will convert your string to int and will use for it just the first digits sequence before "," which is actually what you want.