MySQL - Delete first character but only if it is a specified value - mysql

I have a table named Table1 that looks like this
ID VALUES
1 050
2 50
3 100
4 010
5 300
I need to update the targeted value through the ID. In this case I want to remove leading 0 where ID is 1. This is the end result below:
ID VALUES
1 50
2 50
3 100
4 010
5 300
Bear in mind that Id 4 is remained the same. As I have only specified Id 1 to remove that zero.
Reason I want to do this way, is because I wan't to learn how to remove the leading zero and at the same time how I can remove the leading zero for a specific ID.

You can use TRIM to update the values in your table:
UPDATE Table1
SET `values` = TRIM(LEADING '0' FROM `values`)
WHERE id = 1;
SELECT * FROM Table1
Output:
id values
1 50
2 50
3 100
4 010
5 300
Demo on dbfiddle

You can try below
update tablename set value=substr(value,2,length(value)-1)
where id=1

Please try this
SELECT `id`,
(CASE
WHEN (LEFT(`values`,1) = 0 AND `id` = 1) THEN SUBSTR(`values`,2)
ELSE
`values`
END
) `values`
FROM `data`
DEMO

Related

Get the average of values in every specific epoch ranges in unix timestamp which returns -1 in specific condition in MySQL

I have a MySQL table which has some records as follows:
unix_timestamp value
1001 2
1003 3
1012 1
1025 5
1040 0
1101 3
1105 4
1130 0
...
I want to compute the average for every 10 epochs to see the following results:
unix_timestamp_range avg_value
1001-1010 2.5
1011-1020 1
1021-1030 5
1031-1040 0
1041-1050 -1
1051-1060 -1
1061-1070 -1
1071-1080 -1
1081-1090 -1
1091-1100 -1
1101-1110 3.5
1111-1120 -1
1121-1130 0
...
I saw some similar answers like enter link description here and enter link description here and enter link description here but these answers are not a solution for my specific question. How can I get the above results?
The easiest way to do this is to use a calendar table. Consider this approach:
SELECT
CONCAT(CAST(cal.ts AS CHAR(50)), '-', CAST(cal.ts + 9 AS CHAR(50))) AS unix_timestamp_range,
CASE WHEN COUNT(t.value) > 0 THEN AVG(t.value) ELSE -1 END AS avg_value
FROM
(
SELECT 1001 AS ts UNION ALL
SELECT 1011 UNION ALL
SELECT 1021 UNION ALL
...
) cal
LEFT JOIN yourTable t
ON t.unix_timestamp BETWEEN cal.ts AND cal.ts + 9
GROUP BY
cal.ts
ORDER BY
cal.ts;
In practice, if you have the need to do this sort of query often, instead of the inline subquery labelled as cal above, you might want to have a full dedicated table representing all timestamp ranges.

Change first 200 record in a column into sequent

What I try to do is to make the first 200 records from a column to start from 1 to 200. After 200 records no changing on values.
The current records look like this
1
2
3
4
4
6
6
...
What I need is to update them to be
1
2
3
4
5
...
200
What sql statement do I need to fix them?
Initialize a user defined variable and do it like this:
SET #rownumber = 0;
UPDATE your_table
SET your_column = (#rownumber := #rownumber + 1)
ORDER BY the_column_that_defines_the_order_of_the_first_200_records
LIMIT 200;

MySQL: Use data from one table to fill a second table using phpMyAdmin

SQL conundrum here.
I want to populate an empty table based on the data in an existing table using phpMyAdmin.
More specifically, I want to use the data in mark to create the data in attempt. The columns in mark are student_number, test_number, attempt_number, question_number and the answer. It's a multiple-choice test analysis tool.
mark (existing)
snum tnum anum qnum answer
----------------------------------------
1 1 1 1 A
1 1 1 2 C
1 1 1 3 D
1 1 2 1 B
1 1 2 2 A
1 1 2 3 C
attempt (to be created)
snum tnum anum period
--------------------------------
1 1 1 2013-1
1 1 2 2013-2
I can get the distinct snum, tnum, anum combinations as follows:
SELECT DISTINCT snum, tnum, anum FROM `mark`
How can I use the results from this call to populate the requisite insert call?
INSERT INTO `attempt` (snum,tnum,anum,period) VALUES (:s,:t,:a,"2013-1")
Ideally, I'd like to auto-complete the period value based on "2013-" plus the anum. I suspect this is not possible, so I'll just select all the anum=1 values, and hardcode the period value (and then repeat for each anum).
Thanks.
Use the "INSERT INTO SELECT" syntax:
INSERT INTO 'attempt'(snum,tnum,anum,period) SELECT DISTINCT snum,tnum,anum,CONCAT('2013-',anum) as period FROM mark;
INSERT INTO `attempt`
(snum,
tnum,
anum,
period)
select DISTINCT
snum,
tnum,
anum,
concat('2013-',anum) as period
from mark;

Store a set of numbers in mysql

I need to store a set of integers in MYSQL. The problem is as : I have an id(integer). Each id is mapped to a set of numbers. The numbers in the set can have values from 1 - 160. So my table should be of structure {id,set}. Also each id'set can have different length (max 40 numbers).
Eg:
id set
1 {2 45 46 67 89 155}
2 {1, 11 12 34 56 67 68 79 80 134 145}
I have tried the SET datatype in MYSQL. I defined it as
CREATE TABLE mytable ( id NUMBER PRIMARY KEY , mycol SET('1','2','3','4'...))
But the problem is the set allows to define only 64 elemnets.
Note: I need options other than creating a mapping table as :
id setno
1 2
1 45
....... and so on
Can anyone suggest another way to store a set of numbers in MYSQL.
Create child table to hold the numbers, one per row:
create table set_number (
set_id int,
num int
);
Then insert your numbers. To get them as one CSV value, use group_concat(), something like:
select t.id, group_concat(s.num) set
from mytable t
join set_nimber s
on t.set_id = s.set_id
group by t.id
The table structure you described is the right way to do.
And its all in the way you present data, create a table where it maps ID into Sento
And ID is a primary key
So that you will have:
ID Sento
1 2
1 45
1 46
...
1 155
And then have an SQL that says:
SELECT ID, GROUP_CONCAT(Sento, SEPARATOR ', ')
FROM Table
GROUP BY ID
Hope this helps

mysql query using condition inside sum

I have a column (*Purchasetype*), userid in video table purchasetype is some how containg values 0,1, 2,3,4,.. etc. I want two sum these value order by userid.
For ex: sum ( purchasetype ) order by userid but I want like this
if purchasetype= 0 then its value is 0.99
if purchasetype =1 or 20 then its value is 3.99
if purchasetype = 3 or 13or 22 then its value is 9.99
so on. Below is complete list
0 ,17= 0.99
1,20=3.99
2=6.99
3,13,22=9.99
4,5,6,7,8,,10,11,12=0.00
14=19.99
15,23=39.99
16,24=59.99
18,21=01.99
19=02.99
else
19.99
i want to sum all the values of purchasetype with their replaced values (given above) order by userid
do we can put condition inside the sum() function of mysql; If its possible then please give me solution , may be this will solve my problem
You would use the aggregate function SUM() and CASE:
select
SUM(CASE purchaseType
WHEN 0 or 17 THEN 0.99
WHEN 1 or 20 THEN 3.99
WHEN 3 or 13 or 22 THEN 9.99
WHEN 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 THEN 0
WHEN 14 THEN 19.99
WHEN 15 or 23 THEN 39.99
WHEN 16 or 24 THEN 59.99
WHEN 18 or 21 THEN 1.99
WHEN 19 THEN 2.99
ELSE 19.99 END) as Total
from yourTable
see SQL Fiddle with Demo
I think the best way is to create table ptPrices:
create table ptPrices (Purchasetype int, Price float);
insert into ptPrices values (0,0.99);
insert into ptPrices values (1,3.99);
....
insert into ptPrices values (19,2.99);
And then use this query:
select sum(isnull(ptPrices.Price,19.99)) from Table
left join ptPrices
on Table.Purchasetype=ptPrices.Purchasetype;
Although this doesn't use SUM from MySQL query, the logic will get the job done
$query = mysqli_query($link, "SELECT * FROM video");
while($row = mysqli_fetch_assoc($query)){
//Then set conditionals
if($row['purchase_type)==0 || $row['purchase_type)==17){
$values[] = 0.99;
//Then your mysqli_query here
}
elseif($row['purchase_type)==1 || $row['purchase_type)==20){
$values[]= 3.99;
}
elseif//Blah blah for all values
}
//After exhausting all the rows, add the SUM
$sum = array_sum($values); //$sum will be equal to the addition of all the vlues of the //array $values