How to select rows that contain specific number in MySQL? [duplicate] - mysql

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have this table and want to select rows that contain exactly "22".
id field
1 22
2 22,24,78
3 1,22,347
4 2,21,22
5 22,222
Select above rows, not below.
6 222
7 21,23
8 220,322

The REGEXP operator comes in handy here:
SELECT *
FROM yourTable
WHERE field REGEXP '[[:<:]]22[[:>:]]';
We can also try using FIND_IN_SET:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('22', field) > 0;
If all else fails, we can use LIKE, but it takes slightly more heavy lifting:
SELECT *
FROM yourTable
WHERE CONCAT(',', field, ',') LIKE '%,22,%';
But in general, it is bad practice to store CSV (comma separated values) in your database tables. It would be better to store each field value on a separate rows, e.g. use this:
id field
1 22
2 22
2 24
2 78
...

select * from where field like '%22%';

Related

How to count the length of column seperated by ',' after using group_concat [duplicate]

This question already has answers here:
How to count items in comma separated list MySQL
(6 answers)
Closed 3 years ago.
I have a table looks like below:
ID path
| 1 YouTube,Newsletter,Social
| 2 YouTube,Newsletter
| 3 YouTube
Now I want to create a column to count the length of the path column. such as below:
ID path count weights
| 1 YouTube,Newsletter,Social 3 0.33
| 2 YouTube,Newsletter 2 0.5
| 3 YouTube 1 1
How do I do this?
I have tried JSON_LENGTH but couldn't get the command working.
PS. essentially I'm trying to replicate a query in PostgreSQL:
' select user_id, channels, 1.0 / array_length(channels, 1) as weights
from (
// ... query for marketing_channels as before)'
I am using MYSQL.
select d.email_entry_id
,d.channels
,JSON_LENGTH(d.channels)
from (
select email_entry_id
,group_concat(attribution_string order by visit_date asc separator ',' ) as channels
from database) d
error message: Error Code: 1370. execute command denied to user 'yb'#'%' for routine 'company.JSON_LENGTH'
Hope the question is clear enough. let me know if i need to clarify anything.
If I followed you correctly, you could simply extend the logic of your existing query (which, by the way, seems to be missing a GROUP BY clause). Instead of querying the aggregated data, it would be simpler to start from the original data, like:
SELECT
email_entry_id,
GROUP_CONCAT(attribution_string ORDER BY visit_date SEPARATOR ',' ) as channels,
COUNT(*) as `count`,
1/COUNT(*) as weight
FROM database
GROUP BY email_entry_id
There is a very common trick to achieve such outcome, demonstrated by following query
SELECT ID, PATH,
(LENGTH(PATH) - LENGTH(REPLACE(PATH, ',', ''))) + 1 COUNT
FROM DATABASE /* OR WHATEVER IS THE TABLE NAME */
The result

How to create equivalent of IN clause but allowing AND of listed values [duplicate]

This question already has answers here:
Select group of rows that match all items in a list
(3 answers)
Closed 5 years ago.
I have a table that contains ID and Keywords I need to create that allow to select where 2 or more keyword are present
Example: Where keyword = 779 AND keyword = 782 should result in 4347
ID Keyword
------------------
4347 779
4347 782
8853 779
8853 780
8853 787
I am using IN clause for letting user choose OR case Where Keyword IN (X,y,Z) is there something simlar Keyword IN (X AND Y AND Z) ?
Assuming if ID 8975 had 7 key words 2 of which were 779 and 782 and you'd want that one as well...
SELECT ID
FROM table
WHERE Keyword in (779,782)
GROUP BY ID
HAVING count(distinct keyword) >= 2

MYSQL - Find rows, where part of search string matches part of value in column

I wasn't able to find this anywhere, here's my problem:
I have a string like '1 2 3 4 5' and then I have a mysql table that has a column, let's call it numbers, that look like this:
numbers
1 2 6 8 9 14
3
1 5 3 6 9
7 8 9 23 44
10
I am trying to find the easiest way (hopefully in a single query) to find the rows, where any of the numbers in my search string (1 or 2 or 3 or 4 or 5) is contained in the numbers column. In the give example I am looking for rows with 1,2 and 3 (since they share numbers with my search string).
I am trying to do this with a single query and no loops.
Thanks!
The best solution would be to get rid of the column containing a list of values, and use a schema where each value is in its own row. Then you can use WHERE number IN (1, 2, 3, 4, 5) and join this with the table containing the rest of the data.
But if you can't change the schema, you can use a regular expression.
SELECT *
FROM yourTable
WHERE numbers REGEXP '[[:<:]](1|2|3|4|5)[[:<:]]'
[[:<:]] and [[:<:]] match the beginning and end of words.
Note that this type of search will be very slow if the table is large, because it's not feasible to index it.
Here is a start point (split string function) : http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ := SplitString(string,delimiter,position)
Create a function so it converts a string to an array := stringSplitted(string,delimiter)
Create a function so it compares two arrays :=arrayIntersect(array1, array2)
SELECT numbers
FROM table
WHERE arrayIntersect(#argument, numbers)
Two function definitions with loops and one single query without any loop
SELECT * FROM MyTable WHERE (numbers LIKE '%1%' OR numbers LIKE '%2%')
or you can also use REGEX something like this
SELECT * FROM events WHERE id REGEXP '5587$'

Sort varchar column in mySql [duplicate]

This question already has answers here:
MySQL 'Order By' - sorting alphanumeric correctly
(20 answers)
Closed 7 years ago.
I am trying to sort one column having values
FMOL1001,
FMOL1004,
FMOL1009,
FMOL10010,
FMOL1003,
FMOL10025
But it is not sorting properly,please help
Try this, but im not sure...
If you made a numeric string like this FMOLXXXX-->>XXX then you can sort XXX:
SELECT column, SUBSTRING(column FROM 5) sort FROM table ORDER by CAST(sort AS UNSIGNED)
Sample data :
id name
-------------
1 FMOL1001
2 FMOL1004
3 FMOL1009
4 FMOL10010
5 FMOL1003
6 FMOL10025
Query :
SELECT id, name
FROM table
ORDER BY LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0') ASC
Output :
id name
-------------
1 FMOL1001
5 FMOL1003
2 FMOL1004
3 FMOL1009
4 FMOL10010
6 FMOL10025
Explanation :
LPAD(SUBSTR(name,5,LENGTH(name)-4),10,'0')
Will produce :
FMOL0000001001
FMOL0000001003
FMOL0000001004
FMOL0000001009
FMOL0000010010
FMOL0000010025

Select query with length issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most efficient T-SQL way to pad a varchar on the left to a certain length?
I have the customer table where the customerID is of length:10
but there are few customer where the length is 3 or 5
ex:
3445
34
789
7800
but the output should be like the following, I need to prefix zero here if length is less than 10
0000003445
0000000034
0000000789
0000007800
Script to get this data is:
select customerID from customer
Try the following code:
Since you are using sql server 2008, you could use replicate function to add zeros to the value..
Select ltrim(right(replicate(0,10) + <column>,10))
from your_table
You can use in this way...
SELECT RIGHT('0000000000' + RTRIM('3445'), 10)
In your case,
SELECT RIGHT('0000000000' + RTRIM(customerID), 10) AS New_CustomerID
FROM Customer
Try this
SELECT RIGHT('0000000000'+`customerID`, 10) FROM `customer`