Using comparison and like together to get results from table - mysql

given below is my table structure
Mobile_Number_Start | Mobile_Number_End
005555 | 006565
016565 | 017575
018585 | 019595
Now I have to search record depending on if user enter mobile number start or end. He can also enter number in either of column but not complete number
Suppose if user enters 01 the record table will show output as
Mobile_Number_Start | Mobile_Number_End
016565 | 017575
018585 | 019595
So for this I can't create a query that works
Here is what my query looks like
Select * from table_name where mobile_number_start is less than value entered in start and mobile_number_end is greater than value in mobilenumberend
I have carried out this operation using findall function of repository by building a specification builder class and passin the specification in
findAll(specification)
I need help in writing query for when user enters a part of mobile number but not complete number

you can use the LIKE operator on your query
SELECT *
FROM table_name
WHERE mobile_number_start LIKE '01%' and mobile_number_end LIKE '%01'

Try this one.
SELECT *
FROM table_name
WHERE CAST(mobile_number_start as CHAR) LIKE '01%'
and CAST(mobile_number_end as CHAR) LIKE '%01'

You can use like operator for comparison,
SELECT * FROM table WHERE start_number LIKE '1%' and end_number LIKE '%1';

Related

How to sum daily resetting data using MySQL

I am attempting to plot data cumulatively from a MySQL table which logs a value, resetting to 0 every day. After selecting the values using select * from table where DateTime BETWEEN DateA AND DateB, the data looks like this: current data. I would like the output to look like this: preferred data, ignoring the daily resets.
As I am a novice in SQL I was unable to find a solution to this. I did, however, obtain the correct output in Matlab using a for loop:
output = data;
for k=1:(size(data, 1)-1)
% check if next value is smaller than current
if data(k+1)<data(k)
% add current value to all subsequent values
output = output + (1:size(data, 1)>k)'.*input(k);
end
end
I would like the final product to connect to a web page, so I am curious if it would be possible obtain a similar result using only SQL. While I have tried using SUM(), I have only been able to sum all values, but I need to add the last value each day to all subsequent values.
Using CTE and comparing dates, you can sum all values each date.
Let's say that table1 below is defined.
create table table1 (col_date date, col_value int);
insert into table1 values
('2020-07-15',1000),
('2020-07-15',2000),
('2020-07-16',1000),
('2020-07-16',3000),
('2020-07-16',4000),
('2020-07-17',1000),
('2020-07-18',2000),
('2020-07-19',1000),
('2020-07-19',1000),
('2020-07-19',2000),
('2020-07-19',3000),
('2020-07-20',4000),
('2020-07-20',5000),
('2020-07-21',6000)
;
In this case, the query looks like this:
with cte1 as (
select col_date, sum(col_value) as col_sum from table1
where col_date between '2020-07-16' and '2020-07-20'
group by col_date
)
select a.col_date, max(a.col_sum), sum(b.col_sum)
from cte1 a inner join cte1 b on a.col_date >= b.col_date
group by a.col_date;
The output is below:
col_date |max(a.col_sum) |sum(b.col_sum)
2020-07-16 |8000 | 8000
2020-07-17 |1000 | 9000
2020-07-18 |2000 |11000
2020-07-19 |7000 |18000
2020-07-20 |9000 |27000
The column of max() is just for reference.

Recursively running a MySQL function

I have a function in MySQL that needs to be run about 50 times (not a set value) in a query. the inputs are currently stored in an array such as
[1,2,3,4,5,6,7,8,9,10]
when executing the MySQL query individually it's working fine, please see below
column_name denotes the column it's getting the data for, in this case, it's a DOUBLE in the database
The second value in the MOD() function is the input I'm supplying MySQL from the aforementioned array
SELECT id, MOD(column_name, 4) AS mod_output
FROM table
HAVING mod_output > 10
To achieve the output I require* the following code works
SELECT id, MOD(column_name, 4) AS mod_output1, MOD(column_name, 5) AS mod_output2, MOD(column_name, 6) AS mod_output3
FROM table
HAVING mod_output1 > 10 AND mod_output2 > 10 AND mod_output3 > 10
However this obviously is extremely dirty, and when having not 3 inputs, but over 50, this will become highly inefficient.
Appart from calling over 50 individual querys, is there a better way to acchieve the same sort (see below) of output?
In escennce i need to supply MySQL with a list of values and have it run MOD() over all of them on a specified column.
The only data I need returned is the id's of the rows that match the MOD() functions output with the specified input (see value 2 of the MOD() function) where the output is less than 10
Please note, MOD() has been used as an example function, however, the final function required *should* be a drop in replacement
example table layout
id | column_name
1 | 0.234977
2 | 0.957739
3 | 2.499387
4 | 48.395777
5 | 9.943782
6 | -39.234894
7 | 23.49859
.....
(The title may be worded wrong, I'm not quite sure how else you'd explain what I'm trying to do here)
Use a join and derived table or temporary table:
SELECT n.n, t.id, MOD(t.column_name, n.n) AS mod_output
FROM table t CROSS JOIN
(SELECT 4 as n UNION ALL SELECT 5 UNION ALL SELECT 6 . . .
) n
WHERE MOD(t.column_name, n.n) > 10;
If you want the results as columns, you can use conditional aggregation afterwards.

SQL:Show column only if has data in it

Suppose I have a table like this:
name | age | experience
abc | 18 | 0
def | 19 | 0
efg | 20 | 0
I want to select the experience column only if any one value is greater than zero.
In this case, my SQL query should return only name and age and not experience.
If experience of lets say "efg" is greater than 0, then query should return name, age and experience.
I have tried following query
SELECT EXISTS (SELECT name,age,experience FROM emp_info )
AND NOT
EXISTS (SELECT experience FROM emp_info WHERE experience=0 );
But it is not working.
Try this:-
IF ((select max(experience)FROM emp_info) > 0)
SELECT name,age,experience FROM emp_info
ELSE
SELECT name,age FROM emp_info
In almost all relational databases, your queries have to return fixed numbers of columns, i.e the same number of columns for all rows. So what you are asking for isn't reasonable. You could probably get something like this to work on Informix due to jagged tables support, but that's the only one I can think of.
Other options you have include serializing to JSON in your query, or generating XML but that's a bit advanced for this and it is not clear this is what you want.
Normally we handle this on the front end, not in the database query.
SELECT name, age, NULLIF(experience, 0) from emp_info
Your question is kind of tricky because returning different set of column depending on the result is maybe not what you want to do, you could do it directly from your code, not from your SQL projection.
This may work :
if(experience > 0)
begin
SELECT name,age,experience FROM emp_info
end
else
begin
SELECT name,age FROM emp_info
end

select distinct values of a replaced selected result

Good day all.
I'm facing a little problem with a mySql query. let's assume we have a table with a coulmn in which the values are pairs, but unified in the same field, so something like this:
id | serviceName
----+-------------
1 | foo - bar
2 | foo - doo
3 | foo - tep
4 | bee - bar
5 | bee - blo
I would like to select distinct the first part of serviceName, in this case foo, bee.
the desired output should be:
foo
bee
in the resultset.
what I've thought right now is something about making a SELECT DISTINCT a FROM REPLACE ( (SELECT serviceName as a FROM tableName), ' - ***', '')
but i'm not really sure if it is possible, and how to make it. I only would like to select the first part of the field, and I would like to take only distinct vlaues of it... it is possible? I need a right direction pointing,, I can make researches by my self.
thanks in advance.
Assuming that you always want to split on a dash -, this should work for you.
SELECT DISTINCT LEFT(serviceName, LOCATE('-', serviceName) - 2) FROM tableName;
SQLFiddle
IS this what you are looking at ?
select substring_index(serviceName,'-',1) as `first_part`
from test
group by `first_part`
DEMO

Sql selection using between operator

In my database table there are two fields named salary_to and salary_from.Now in front end of my website user can enter a salary in an input field say 20000.Now i wanna perform a check in the database that if any row have salaryto and salaryfrom between which the value '20000' lies.
My desired algo looks like something like this
SELECT all ROW where salaryto is less than 20000 and salaryfrom is greater than 20000
Can i use the between operator to perform this kind of filterring or i just ave to use >,< operator to this?
Provided that I'm using mysql database for my project.
The between operator is INCLUSIVE meaning it includes the START and END Value. While < > are exclusive.
SELECT *
FROM tableName
WHERE salaryto < 20000 and salaryfrom > 20000
So the rows that are fetched here are with salaryto value of 19999 and below AND salaryfrom value of 20001 and above, assuming that it increases by 1.
You can use this:
SELECT * FROM yourTable WHERE 2000 BETWEEN salary_to AND salary_from
But this is Recommended:
SELECT * FROM yourTable WHERE (salary_to < 2000) and (salary_from > 2000)