Treat number as Zero in mysql query - mysql

If have rows table like this
id content
1 this five lenght is 12345
2 this five lenght is 23456
3 this six lenght is 234567
4 this six lenght is 238567
Then when I want to select and group by content, it will result
SELECT * FROM table GROUP BY content
1 this five lenght is 00000
3 this six lenght is 000000
Is there a way to achieve this, that can replace number with zero in mysql query?
Many thanks in advance.

Replace numbers with zero and then group them.
Example for replacement:
Select REGEXP_REPLACE('Stackoverflow 2456','[0-9]','0')
Stackoverflow 0000
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=db93de9c1c965090f46b4dbb1f48a63e
In your case:
SELECT REGEXP_REPLACE(CONTENT, '[0-9]','0') FROM TABLENAME GROUP BY REGEXP_REPLACE(CONTENT, '[0-9]','0')
Be careful tho this will probably do a full TABLE scan, so it will be really slow on large tables.

You can try with REGEXP_REPLACE() function - it'll work on mysql 8+ version
SELECT id, REGEXP_REPLACE (content,'[0-9]', '0') as content
FROM tablename GROUP BY REGEXP_REPLACE (content,'[0-9]', '0')

Related

MySQL View decimal place

I have a MySQL view called Balance created from 2 tables order and income with PHPMyAdmin and contains some calculated fields ex: CustomerBalance the decimal place become automatically 8, I mean the field Type is decimal(50,8)
How can i make it 2 only ?
You can use truncate
SELECT TRUNCATE(1.999,2);
return 1.99
select TRUNCATE(your_column,2) from your_table;
In the select list where you calculate the CustomerBalance expression, explicitly truncate or round (depending on your requirements) the result to 2 digits:
select ... round(..., 2) as CustomerBalance ...

SQL stament groups rows and calculate average

I am stuck with the following issue. I have 1 table that looks like this:
field_number.. Value
````````````````````````````````
1 ......................... 1
2 ..........................1
3 ......................... 2
4 ..........................2
etc.
I want to group different fieldnumbers and have an average for the value column. So the output should be:
field_number................Value
name(1,2)...................... 1.............. ((1+1)/2)
name(3,4)...................... 2.............. ((2+2)/2)
I have checked previous questions but cannot find any question that covers this issue (I might search on the wrong keywords though). So if this has already been covered my appologies, but any help or a point to a previous answer would be appreciated.
** =============UPDATE============= **
I went through your suggestions but did not get it right. So I am trying to be more specific. I almost have the result I want apart from the fact I want to have a fixed value in one of my columns. I have the following query:
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (15, 17, 24) A
UNION
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (16, 108, 18)
etc.
This gives me a table with two columns
wp_rg_lead_detail.value................field_number
4.3 (average)..............................15 (first value of av calculation)
What I want is to change the field number (15 in this case) in a fixed value (text). What and how should I add this to the query?
SELECT avg(value) FROM table WHERE field_number in (1,2)
SELECT avg(value) FROM table WHERE field_number in (3,4)
If your table is really this simple, you can also get away with:
select distinct
Value,
count(Value) as '#'
from table_name
group by Value
If you acctually want to group by a range, than you can put the logic of the range in your grouping clause (see this fiddle)
select distinct
avg(Value) as average,
floor(Value),
count(Value) as '#'
from table_name
group by floor(Value)
In the fiddle I used grouping on whole integers, but you can make that as complex as you like (see, for instance, this example)
If you are actually also interested in your corresponding fields, use group_concat() like so
select
Value,
group_concat(
distinct field_number
order by Value
) as fields
from table_name tn1
group by Value
order by Value
output:
Value | fields
---------------------------------
1 | 1,2
2 | 3,4
See this fiddle implemented from this blog post
For a generalized answer.
SELECT CONCAT('name','(',GROUP_CONCAT(field_number),')') AS field_number,
AVG(Value) as Value
FROM table_name
group by table_name.`Value`
Hope this helps.

Mysql select to get only part of a row from a column

I have a table with a column that contains a string of numbers and I only want to return the last couple of digits.
For example:
column1 | column2
_________________
Blah | 1231357
I need a select that will return the last couple of digits from the second column.
Use the RIGHT function:
SELECT RIGHT(column2, 3) AS LastDigits FROM TableName
Change 3 to the number of digits you want.
A modulus operator will take only the last two digits.
SELECT MOD(column2, 100) FROM mytable
Change 100 to 1000 to get three digits, etc.

mysql query - pairs of repeating numbers

I have a database with 2 columns (ex: no_1, no_2) and then 5000 rows of data, numbers are between 1 - 20 , I need to find a pairs of numbers which where repeating the most of the times?
Any help please ?
Something like this maybe:-
SELECT no_1, no_2, COUNT(*) AS numbercount
FROM SomeTable
GROUP BY no_1, no_2
ORDER BY numbercount DESC

mysql sort varchar with numbers

How can I sort this data numerically rather than lexicographically?
100_10A
100_10B
100_10C
100_11A
100_11B
100_11C
100_12A
100_12B
100_12C
100_13A
100_13B
100_13C
100_14A
100_14B
100_14C
100_15A
100_15B
100_15C
100_16A
100_16B
100_16C
100_1A
100_1B
100_1C
100_2A
100_2B
100_2C
100_3A
100_3B
100_3C
100_4A
100_4B
100_4C
100_5A
100_5B
100_5C
100_6A
100_6B
100_6C
100_7A
100_7B
100_7C
100_8A
100_8B
100_8C
100_9A
100_9B
100_9C
select generalcolum from mytable order by blockid, plotid ASC
What I need out of this sort order is
100_1A
100_1B
100_1C...
...
...
100_10A
100_10B
100_10C
What I need to do in some way is have a zero added before the sort happens so that, I can get them in the order I want.
There are two colums, one that stores the 100 (number before the underscore) and one that stores the 1A the value after the underscore.
My sudo crap select
select thiscolum this table
order by blockid, plotid(+1 zero to prefix if len(plotid) < 2)
For example if the plot value is 1A, to do the best sorting, i need it to be looked at as 01A so that it comes before 10A.
order by length(blockid), blockid, length(plotid), plotid