Sort varchar column in mySql [duplicate] - mysql

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

Related

to Search 11 in [1,2,10,11,111,112] by SQL(mysql) [duplicate]

This question already has answers here:
How to search JSON array in MySQL?
(11 answers)
Closed 2 months ago.
I have a column in a table that contains some number offsets like this:
my_offsets
[1,2,10,11,111,112]
I want query this rows for search value 11 in [1,2,10,11,111,112]. How can I do in SQL(mysql)?
if data is table
SELECT my_offsets
FROM my_tab
WHERE my_offsets='11'
if data is JSON
SELECT * from my_table
WHERE JSON_CONTAINS(yur_data, '11', '$');
example
JSON_CONTAINS('[1,2,3]','3','$') Returns: 1
JSON_CONTAINS('[1,2,3]','7','$') Returns: 0

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 select rows that contain specific number in MySQL? [duplicate]

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%';

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

Multiple aggregate function for mysql. like SUM [duplicate]

This question already has answers here:
Get rows product (multiplication)
(3 answers)
Closed 9 years ago.
How can I multiple numbers from table to one result like SUM() does?
In "table1" I have column "number1" with these values:
table1.number1
--------------
1
2
3
I try this sql:
SELECT #multiple := #multiple number1 as mul
FROM table1
and I got this:
mul
---
1
2
6
But I need just the last row with the value: 6
without using ORDER DESC
Try this:
SELECT MAX(#multiple := #multiple * number1) AS mul
FROM table1, (SELECT #multiple:=1) a;